{"id":22513985,"url":"https://github.com/emahtab/binary-tree-preorder-traversal","last_synced_at":"2026-01-07T02:37:32.499Z","repository":{"id":79525306,"uuid":"233342491","full_name":"eMahtab/binary-tree-preorder-traversal","owner":"eMahtab","description":"Binary Tree Preorder Traversal","archived":false,"fork":false,"pushed_at":"2020-02-06T08:38:50.000Z","size":45,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-02T03:26:17.173Z","etag":null,"topics":["binary-tree","leetcode","preorder-traversal","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-12T05:20:13.000Z","updated_at":"2020-02-06T08:38:52.000Z","dependencies_parsed_at":"2023-05-10T17:00:29.994Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/binary-tree-preorder-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-preorder-traversal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-preorder-traversal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-preorder-traversal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-preorder-traversal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/binary-tree-preorder-traversal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952344,"owners_count":20699472,"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","leetcode","preorder-traversal","problem-solving"],"created_at":"2024-12-07T03:15:29.260Z","updated_at":"2026-01-07T02:37:32.470Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Binary Tree Preorder Traversal 🌲\n## https://leetcode.com/problems/binary-tree-preorder-traversal\n\nGiven a binary tree, return the preorder traversal of its nodes' values.\n```\nExample:\n\nInput: [1,null,2,3]\n   1\n    \\\n     2\n    /\n   3\n\nOutput: [1,2,3]\n```\n**Follow up: Recursive solution is trivial, could you do it iteratively?**\n\n# Preorder 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 preorder traversal for above binary tree will be **[70, 67, 35, 21, 20, 75, 30, 43, 29, 50, 24, 60, 65]**\n\n\n## Implementation : Recursive\n\n```java\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class App {\n\n     public 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     }\n\t\n\t\n     // Definition for a binary tree node.\n     static public class TreeNode {\n\t      int val;\n\t      TreeNode left;\n\t      TreeNode right;\n\t      TreeNode(int x) { val = x; }\n     }\n\t \n\t\n     public 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        if (root != null) {\n             res.add(root.val);\n             helper(root.left, res);\n             helper(root.right, res);\n        }\n     }\n}\n\n```\n## Implementation : Iterative (Stacks are awesome ...)\n\n```java\npublic List\u003cInteger\u003e preorderTraversal(TreeNode root) {\n       List\u003cInteger\u003e res = new ArrayList\u003c\u003e(); \n       if (root == null) \n            return res;\n\n       Stack\u003cTreeNode\u003e stack = new Stack\u003c\u003e();\n       stack.push(root);\n\n       while (!stack.empty()) {\n            TreeNode curr = stack.pop();\n            res.add(curr.val);\n\t    \n            if (curr.right != null) {\n                stack.push(curr.right);\n            }\n           \n            if (curr.left != null) {\n                stack.push(curr.left);\n            }\n      }\n    return res;\n}\n```\n\n# References :\nhttps://algorithmsandme.com/iterative-preorder-traversal\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-preorder-traversal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fbinary-tree-preorder-traversal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-preorder-traversal/lists"}