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 }

 

posted @ 2016-07-08 07:23  Joyce-Lee  阅读(195)  评论(0编辑  收藏  举报