public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if (root == null)
return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.empty()) {
TreeNode tmp = stack.pop();
list.add(tmp.val);
if (tmp.right != null)
stack.push(tmp.right);
if (tmp.left != null)
stack.push(tmp.left);
}
return list;
}
}
果然比递归难了很多。