public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ilist=new ArrayList<Integer>();
Deque<TreeNode> stack=new ArrayDeque<TreeNode>();
TreeNode p=root;
if(root==null)
return ilist;
//stack.push(root)
while((p!=null) || !stack.isEmpty()){
while(p!=null){
stack.addFirst(p);
ilist.add(p.val);
p=p.left;
}
p=stack.removeFirst();
p=p.right;
}
return ilist;
}
}
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ilist=new ArrayList<Integer>();
Deque<TreeNode> stack=new ArrayDeque<TreeNode>();
TreeNode p=root;
if(root==null)
return ilist;
//stack.push(root)
while((p!=null) || !stack.isEmpty()){
while(p!=null){
stack.addFirst(p);
ilist.add(p.val);
p=p.left;
}
p=stack.removeFirst();
p=p.right;
}
return ilist;
}
}