Binary Tree Traversal

1. Preorder Tree Traversal

 1 // Solution 1. With Stack
 2 // Time Complexity : O(n)
 3 // Space Complexity: O(h) ~ O(log n)
 4 public class Solution {
 5     public List<Integer> preorderTraversal(TreeNode root) {
 6         ArrayList<Integer> list = new ArrayList<Integer>();
 7         if (root == null) return list;
 8
 9         Stack<TreeNode> stack = new Stack<TreeNode>();
10         stack.push(root);
11         TreeNode cur = null;
12         while (!stack.isEmpty()) {
13             cur = stack.pop();
14             list.add(cur.val); // Do manipulation here
15             if (cur.right != null) stack.push(cur.right);
16             if (cur.left != null) stack.push(cur.left);
17         }
18
19         return list;
20     }
21 }
 1 // Solution 2. Morris Traversal
 2 // Time Complexity : O(n)
 3 // Space Complexity : O(1)
 4 public class Solution {
 5     public List<Integer> preorderTraversal(TreeNode root) {
 6         List<Integer> list = new ArrayList<Integer>();
 7
 8         TreeNode cur = root;
 9         TreeNode pre = null;
10         while (cur != null) {
11             if (cur.left != null) {
12                 pre = cur.left;
13                 while (pre.right != null && pre.right != cur) { // link / unlink
14                     pre = pre.right;
15                 }
16                 if (pre.right == null) {
17                     list.add(cur.val);
18                     pre.right = cur;
19                     cur = cur.left;
20                 } else {
21                     pre.right = null;
22                     cur = cur.right;
23                 }
24             } else {
25                 list.add(cur.val);
26                 cur = cur.right;
27             }
28         }
29
30         return list;
31     }
32 }

2. Inorder Tree Traversal

 1 // Solution 1. With Stack
 2 // Time Complexity : O(n)
 3 // Space Complexity: O(h) ~ O(log n)
 4 public class Solution {
 5     public List<Integer> inorderTraversal(TreeNode root) {
 6         ArrayList<Integer> list = new ArrayList<Integer>();
 7         if (root == null) return list;
 8
 9         TreeNode cur = root;
10         Stack<TreeNode> stack = new Stack<TreeNode>();
11         while (cur != null || !stack.isEmpty()) {
12             if (cur != null) {
13                 stack.push(cur);
14                 cur = cur.left;
15             } else {
16                 cur = stack.pop();
17                 list.add(cur.val); // Do manipulation here
18                 cur = cur.right;
19             }
20         }
21
22         return list;
23     }
24 }
 1 // Solution 2. Morris Traversal
 2 // Time Complexity : O(n)
 3 // Space Complexity : O(1)
 4 public class Solution {
 5     public List<Integer> inorderTraversal(TreeNode root) {
 6         List<Integer> list = new ArrayList<Integer>();
 7
 8         TreeNode cur = root;
 9         TreeNode pre = null;
10         while (cur != null) {
11             if (cur.left != null) {
12                 pre = cur.left;
13                 while (pre.right != null && pre.right != cur) {
14                     pre = pre.right;
15                 }
16                 if (pre.right == null) {
17                     pre.right = cur;
18                     cur = cur.left;
19                 } else {
20                     pre.right = null;
21                     list.add(cur.val);
22                     cur = cur.right;
23                 }
24             } else {
25                 list.add(cur.val);
26                 cur = cur.right;
27             }
28         }
29         return list;
30     }
31 }

3. Postorder Tree Traversal

 1 // Solution 1. With Stack
 2 // Time Complexity : O(n)
 3 // Space Complexity: O(h) ~ O(log n)
 4 public class Solution {
 5     public List<Integer> postorderTraversal(TreeNode root) {
 6         ArrayList<Integer> list = new ArrayList<Integer>();
 7         if (root == null) return list;
 8
 9         TreeNode pre = null;
10         TreeNode cur = root;
11         Stack<TreeNode> stack = new Stack<TreeNode>();
12         while (cur != null || !stack.isEmpty()) {
13             if (cur != null) {
14                 stack.push(cur);
15                 cur = cur.left;
16             } else {
17                 cur = stack.pop();
18                 if (cur.right == null || pre == cur.right) { // won‘t put back
19                     list.add(cur.val); // Do manipulation here
20                     pre = cur;
21                     cur = null;
22                 } else {
23                     stack.push(cur);
24                     cur = cur.right;
25                 }
26             }
27         }
28
29         return list;
30     }
31 }
 1 // Solution 2. Morris Traversal
 2 // Time Complexity : O(n)
 3 // Space Complexity : O(1)
 4 public class Solution {
 5     public List<Integer> postorderTraversal(TreeNode root) {
 6         List<Integer> list = new ArrayList<Integer>();
 7
 8         TreeNode dump = new TreeNode(-1);
 9         dump.left = root;
10
11         TreeNode cur = dump;
12         TreeNode pre = null;
13         while (cur != null) {
14             if (cur.left != null) {
15                 pre = cur.left;
16                 while (pre.right != null && pre.right != cur) {
17                     pre = pre.right;
18                 }
19                 if (pre.right == null) {
20                     pre.right = cur;
21                     cur = cur.left;
22                 } else {
23                     List<Integer> rev = new ArrayList<Integer>();
24                     TreeNode tmp = cur.left;
25                     while (tmp != cur) {
26                         rev.add(tmp.val);
27                         tmp = tmp.right;
28                     }
29                     if (!rev.isEmpty()) {
30                         Collections.reverse(rev); // Collections.reverse return void
31                         list.addAll(rev);
32                     }
33                     pre.right = null;
34                     cur = cur.right;
35                 }
36             } else {
37                     cur = cur.right;
38             }
39         }
40         return list;
41     }
42 }
时间: 2024-10-10 17:53:38

Binary Tree Traversal的相关文章

C++ Binary Tree Traversal

关于二叉树的遍历有很多的方法, 下面介绍两个经典的遍历算法: BFS和DFS.一个是深度优先遍历, 一个是广度有优先遍历. 这两种遍历算法均属于盲目的遍历算法, 一般而言, 启发式的遍历搜索算法比较好一些. . 关于各种遍历算法的对比, 将会在后面逐一提及. 这里不在赘述. 由于树是一个非线性的数据结构, 显然不能像linked list , 或者Array那样通过从头像最末尾移动去实现遍历每一个元素, 我们使用的是BFS和DFS算法. 例如下面的一个二叉树: 所谓的树遍历(Tree trave

【Leetcode】Binary Tree Traversal

把三个二叉树遍历的题放在一起了. 递归写法太简单,就不再实现了,每题实现了两种非递归算法. 一种是利用栈,时间和空间复杂度都是O(n). 另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点的空指针域. 先序: Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,

LeetCode—— Binary Tree Traversal

二叉树遍历的三道题 Binary Tree Preorder Traversal Binary Tree Inorder Traversal Binary Tree Postorder Traversal LeetCode 上二叉树的节点定义如下: // 树的节点 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {

Binary Tree Traversal Algorithms (二叉树遍历算法)

本文共列出了11个常见的二叉树遍历算法.二叉树的遍历主要有深度优先遍历和广度优先遍历.深度优先遍历包含前序遍历.中序遍历和后序遍历. 值得一提的是, 其中的 Morris 算法 可以线性时间不需要额外空间(用户栈或系统栈空间)实现二叉树的前序遍历.中序遍历和后序遍历.关于Morris算法, 可参考 http://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html 算法清单: A1. A2. A3, 分别是中序遍历的递归

Binary Tree Traversal 二叉树的前中后序遍历

[抄题]: [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [五刷]: [总结]: [复杂度]:Time complexity: O() Space complexity: O() [英文数据结构,为什么不用别的数据结构]: [其他解法]: [Follow Up]: [L

Binary Tree Traversal In Three Ways In Leetcode

144. 二叉树的前序遍历 class Solution { public: vector<int> ans; //递归版本 /* void previsit(TreeNode* r) { if(r != NULL) { ans.push_back(r->val); previsit(r->left); previsit(r->right); } }*/ // 非递归版本 借助于栈 /* void previsit(TreeNode* r) { stack<TreeNo

[lintcode easy]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Challenge Can you do it without recursion? Among preorder,inorder and postorder binary tree

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node