原题链接在这里:https://leetcode.com/problems/binary-tree-inorder-traversal/#
题目:
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
1 \ 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
题解:
类似Binary Tree Preorder Traversal, Binary Tree Postorder Traversal.
可以分别采用Recursion, Iteration 和 Morris Traversal 三种方法。
Method 1: Recursion
Recursion是traversal时最容易想到,并且好写的方法。
Time Complexity: O(n). Space O(logn).
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public List<Integer> inorderTraversal(TreeNode root) { 12 List<Integer> ls = new ArrayList<Integer>(); 13 inorderTraversal(root, ls); 14 return ls; 15 } 16 public void inorderTraversal(TreeNode root, List<Integer> ls){ 17 if(root == null){ 18 return; 19 } 20 inorderTraversal(root.left, ls); 21 ls.add(root.val); 22 inorderTraversal(root.right, ls); 23 } 24 }
Method 2: Iteration + Stack
Iteration 时基本就是利用stack的特性体现出recursion的方法。压栈的顺序要注意,一直压left, 直到null, 再pop, 移动root到pop出来的right. 循环直到点为空并且stack也为空。
Time Complexity: O(n). Space O(logn).
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public List<Integer> inorderTraversal(TreeNode root) { 12 List<Integer> res = new ArrayList<Integer>(); 13 if(root == null){ 14 return res; 15 } 16 Stack<TreeNode> stk = new Stack<TreeNode>(); 17 while(root != null || !stk.isEmpty()){ 18 if(root != null){ 19 stk.push(root); 20 root = root.left; 21 }else{ 22 TreeNode tn = stk.pop(); 23 res.add(tn.val); 24 root = tn.right; 25 } 26 } 27 return res; 28 } 29 }
Method 3:
Morris Traversal 算法,参见了Morris Traversal这篇文章。
Time Complexity: O(n). Space O(1).利用 constant space traverse.
基本思路就是:
1. 看当前点cur的left是否为null, 若是null, add当前节点, cur = cur.right.
2. cur.left 不为 null, 先在左子树中找到cur 的predecessor, 然后分两种情况考虑:
a. predecessor.right = null, 表明左子树还没遍历过。那么predecessor.right 指向 cur; cur = cur.left.
b. predecessor.right = cur, 表明左子树已经遍历完了。那么先add cur, 然后把predecessor.right 改回null, 恢复树的结构,然后cur = cur.right.
Note: 1. while()里的条件和下面的if()条件是相呼应的. e.g while()里若写a.next != null, 下面的if()里就应该写a.next 的先关特性。
2. 代码看起来是两个while循环嵌套,感觉是Time 是O(n*logn),其实每个边最多被访问2次,edge的上限是n-1,所以Time is O(n).
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public List<Integer> inorderTraversal(TreeNode root) { 12 List<Integer> res = new ArrayList<Integer>(); 13 TreeNode cur = root; 14 TreeNode pre = null; 15 while(cur != null){ 16 if(cur.left == null){ 17 res.add(cur.val); 18 cur = cur.right; 19 }else{ 20 //Left node is not null, find predcessor 21 pre = cur.left; 22 while(pre.right != null && pre.right != cur){ 23 pre = pre.right; 24 25 } 26 27 if(pre.right == null){ 28 pre.right = cur; 29 cur = cur.left; 30 }else{ 31 res.add(cur.val); 32 pre.right = null; 33 cur = cur.right; 34 } 35 } 36 } 37 return res; 38 } 39 }