144. Binary Tree Preorder Traversal
- Visit and Push all the way left
- cur = pop()
- cur = cur.right (then go back to 1 and continue)
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> returnList = new ArrayList<Integer>();
if(root == null)
{
return returnList;
}
Deque<TreeNode> stack = new LinkedList<TreeNode>();
TreeNode cur = root;
while(cur!=null || !stack.isEmpty()){
while(cur!=null)
{
returnList.add(cur.val);
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
cur = cur.right;
}
return returnList;
}
}