package com.leetcode;
import java.util.ArrayList;
import java.util.Stack;
public class PreorderTraversal {
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (root == null)
return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
// stack.push(root);
while (!stack.isEmpty() || root != null) {
if (root == null) {
TreeNode p = stack.pop();
root = p.right;
} else {
list.add(root.val);
stack.push(root);
root = root.left;
}
}
return list;
}
}