/*
stack 解法
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
List<Integer> rst = new ArrayList<Integer>();
if (root == null) {
return rst;
}
TreeNode curr = root;
while (curr != null || !stack.isEmpty()) {
while (curr != null) {
stack.add(curr);
curr = curr.left;
}
curr = stack.peek();
stack.pop();
rst.add(curr.val);
curr = curr.right;
}
return rst;
}
}
/*
divide and conquer
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> rst = new ArrayList<Integer>();
if (root == null) {
return rst;
}
List<Integer> left = inorderTraversal(root.left);
List<Integer> right = inorderTraversal(root.right);
rst.addAll(left);
rst.add(root.val);
rst.addAll(right);
return rst;
}
}