Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
这篇是说要用前序遍历一棵树,那么什么是前序呢?请看这里→http://baike.baidu.com/view/1455146.htm
代码如下,看不懂看注释啦~~~
public class BinaryTreePreorderTraversal {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> tree = new ArrayList<Integer>();
if(root==null) return tree;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p= root;
stack.push(p);
while(!stack.empty()){
TreeNode tmp = stack.pop();//先把根加进去
tree.add(tmp.val);
if(tmp.right!=null)//如果有right,先把右边加进去,但是沉在stack底部,下一个循环出不来直到所有left都遍历完
stack.push(tmp.right);
if(tmp.left!=null)//再来left,下一次就会pop出来加进去,然后再判断它的right和left
stack.push(tmp.left);
}
return tree;
}
}