{"id":22513983,"url":"https://github.com/emahtab/binary-tree-inorder-traversal","last_synced_at":"2026-01-06T08:44:37.304Z","repository":{"id":79525330,"uuid":"233339916","full_name":"eMahtab/binary-tree-inorder-traversal","owner":"eMahtab","description":"Binary Tree Inorder Traversal","archived":false,"fork":false,"pushed_at":"2020-02-06T08:38:08.000Z","size":55,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:26:17.086Z","etag":null,"topics":["binary-tree","inorder-traversal","leetcode","problem-solving"],"latest_commit_sha":null,"homepage":"","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}},"created_at":"2020-01-12T04:51:29.000Z","updated_at":"2020-02-06T08:38:10.000Z","dependencies_parsed_at":"2023-05-10T17:15:30.152Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/binary-tree-inorder-traversal","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-inorder-traversal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-inorder-traversal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-inorder-traversal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-inorder-traversal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/binary-tree-inorder-traversal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952418,"owners_count":20699482,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["binary-tree","inorder-traversal","leetcode","problem-solving"],"created_at":"2024-12-07T03:15:29.198Z","updated_at":"2026-01-06T08:44:37.275Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Binary Tree Inorder Traversal 🌲\n## https://leetcode.com/problems/binary-tree-inorder-traversal\nGiven a binary tree, return the inorder traversal of its nodes' values.\n\n```\nExample:\n\nInput: [1,null,2,3]\n   1\n    \\\n     2\n    /\n   3\n\nOutput: [1,3,2]\n```\n**Follow up: Recursive solution is trivial, could you do it iteratively?**\n\n# Inorder Traversal :\n![Binary Tree](binary-tree.PNG?raw=true \"Binary Tree\")\n\nNote that node 75 doesn't have left child and node 29 doesn't have right child.\n\nThe inorder traversal for above binary tree will be **[21, 35, 20, 67, 75, 30, 70, 50, 29, 43, 60, 24, 65]**\n\n## Implementation : Recursive\n\n```java\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class App {\n\n\tpublic static void main(String[] args) {\n\t\tTreeNode root = new TreeNode(70);\n\t\troot.left = new TreeNode(67); root.right = new TreeNode(43);\n\t\t\n\t\troot.left.left = new TreeNode(35); root.left.right = new TreeNode(75); \n\t\t\n\t\troot.left.left.left = new TreeNode(21); root.left.left.right = new TreeNode(20);\n\t\t\n\t\troot.left.right.right = new TreeNode(30);\n\t\t\n\t\troot.right.left = new TreeNode(29); root.right.left.left = new TreeNode(50);\n\t\t\n\t\troot.right.right = new TreeNode(24); \n\t\t\n\t\troot.right.right.left = new TreeNode(60); root.right.right.right = new TreeNode(65);\n\t\tSystem.out.println(inorderTraversal(root));\n\t}\n\t\n\t\n\t// Definition for a binary tree node.\n\tstatic public class TreeNode {\n\t      int val;\n\t      TreeNode left;\n\t      TreeNode right;\n\t      TreeNode(int x) { val = x; }\n\t}\n\t \n\t\n\tpublic static List \u003cInteger\u003e inorderTraversal(TreeNode root) {\n            List \u003cInteger\u003e res = new ArrayList\u003cInteger\u003e();\n            helper(root, res);\n            return res;\n        }\n\n       public static void helper(TreeNode root, List \u003cInteger\u003e res) {\n\t    if (root != null) {\n\t\t  helper(root.left, res);\n\t\t  res.add(root.val);\n\t\t  helper(root.right, res);\n\t      }\n       }\n}\n\n```\n## Implementation : Iterative (Simple and awesome ...)\n\n```java\npublic List \u003cInteger\u003e inorderTraversal(TreeNode root) {\n      List \u003cInteger\u003e res = new ArrayList\u003cInteger\u003e();\n      Stack\u003cTreeNode\u003e stack = new Stack();\n      TreeNode curr = root;\n\n      // if current node is null and stack is also empty, we're done\n      while (!stack.empty() || curr != null) {\n          // if current node is not null, push it to the stack (defer it)and move to its left child\n            if (curr != null) {\n                stack.push(curr);\n                curr = curr.left;\n            } else {\n                // else if current node is null, we pop an element from stack,\n                // print it and finally set current node to its right child\n                curr = stack.pop();\n                res.add(curr.val);\n                curr = curr.right;\n            }\n      }\n   return res;\n}\n```\n\n# References :\n1. https://leetcode.com/problems/binary-tree-inorder-traversal/solution\n2. https://www.youtube.com/watch?v=50v1sJkjxoc (Iterative Approach using Stack)\n3. https://www.techiedelight.com/inorder-tree-traversal-iterative-recursive\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-inorder-traversal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fbinary-tree-inorder-traversal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-inorder-traversal/lists"}