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]
.
思路:和上题一样,还是不用递归,用stack实现
代码如下(已通过leetcode)
public static void inorder(List<Integer> list,TreeNode root) {
if(root==null) return;
Stack<TreeNode> stack=new Stack<TreeNode>();
while(root!=null||!stack.isEmpty()) {
while(root!=null) {
stack.push(root);
root=root.left;
}
root=stack.pop();
list.add(root.val);
root=root.right;
}
}