Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 2 / 3
return [1,3,2]
.
public class Solution { public ArrayList<Integer> inorderTraversal(TreeNode root) { ArrayList<Integer> re = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode cur = root; while(cur!=null||!stack.isEmpty()){ if(cur!=null){ stack.push(cur); cur = cur.left; }else{ re.add(stack.peek().val); cur = stack.peek().right; stack.pop(); } } return re; } }