标签:
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
if(root == null)
return ret;
ArrayDeque<TreeNode> toProc = new ArrayDeque<TreeNode>();
TreeNode node = root;
while(node!=null)
{
ret.add(node.val);
if(node.right != null)
toProc.push(node.right);
if(node.left != null)
node = node.left;
else
{
if(toProc.isEmpty())
break;
node = toProc.pop();
}
}
return ret;
}
Binary Tree Preorder Traversal
标签:
原文地址:http://www.cnblogs.com/neweracoding/p/4825089.html