{"id":22513742,"url":"https://github.com/emahtab/flatten-binary-tree-to-linked-list","last_synced_at":"2026-01-31T03:31:04.971Z","repository":{"id":79525508,"uuid":"239076292","full_name":"eMahtab/flatten-binary-tree-to-linked-list","owner":"eMahtab","description":"Flatten a Binary Tree to Linked List","archived":false,"fork":false,"pushed_at":"2020-02-08T05:53:41.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-11T02:50:57.214Z","etag":null,"topics":["flatten","flatten-binary-tree","leetcode","preorder","problem-solving"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-02-08T05:42:19.000Z","updated_at":"2020-02-08T05:53:43.000Z","dependencies_parsed_at":"2023-05-10T17:16:24.379Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/flatten-binary-tree-to-linked-list","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/flatten-binary-tree-to-linked-list","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fflatten-binary-tree-to-linked-list","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fflatten-binary-tree-to-linked-list/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fflatten-binary-tree-to-linked-list/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fflatten-binary-tree-to-linked-list/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/flatten-binary-tree-to-linked-list/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fflatten-binary-tree-to-linked-list/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28928148,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-31T02:59:34.861Z","status":"ssl_error","status_checked_at":"2026-01-31T02:59:05.369Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flatten","flatten-binary-tree","leetcode","preorder","problem-solving"],"created_at":"2024-12-07T03:14:20.590Z","updated_at":"2026-01-31T03:31:04.965Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flatten a Binary Tree to Linked List\n## https://leetcode.com/problems/flatten-binary-tree-to-linked-list\n\nGiven a binary tree, flatten it to a linked list in-place.\n```\nFor example, given the following tree:\n\n    1\n   / \\\n  2   5\n / \\   \\\n3   4   6\n\nThe flattened tree should look like:\n\n1\n \\\n  2\n   \\\n    3\n     \\\n      4\n       \\\n        5\n         \\\n          6\n```\n\n# Implementation :\n\n```java\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode(int x) { val = x; }\n * }\n */\nclass Solution {\n    public void flatten(TreeNode root) {\n        if(root == null)\n            return;\n        Stack\u003cTreeNode\u003e stack = new Stack\u003c\u003e();\n        stack.push(root);\n        while(!stack.isEmpty()){\n            TreeNode current = stack.pop();\n            if(current.right != null)\n                stack.push(current.right);\n            if(current.left != null)\n                stack.push(current.left);\n            \n            if(!stack.isEmpty())\n               current.right = stack.peek();\n            current.left = null;\n        } \n    }\n    \n}\n```\n\n### ❗️ 💥 Caution : EmptyStackException\nCalling `stack.pop()` or `stack.peek()` on an empty stack will result in a EmptyStackException. So always make sure you don't call those method on an empty stack. In above implementation when there is only one `TreeNode` in the stack and we pop it, and the popped TreeNode doesn't have right and left child, so now there will be nothing in the stack, so calling `stack.peek()` will result in EmptyStackException. So we add the check of, if the stack is not empty, then only we call `stack.peek()`.\n\n\n\n# References :\nhttps://www.youtube.com/watch?v=vssbwPkarPQ\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fflatten-binary-tree-to-linked-list","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fflatten-binary-tree-to-linked-list","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fflatten-binary-tree-to-linked-list/lists"}