LeetCode 144. 二叉树的前序遍历

二叉树的前序遍历(迭代法)
 1 class Solution {
 2     public List<Integer> preorderTraversal(TreeNode root) {
 3         //前序遍历非递归法需要借助一个栈
 4         Stack<TreeNode> stack = new Stack<>();
 5         //定义返回的结果集
 6         List<Integer> res = new LinkedList();
 7 
 8         if(root == null) return res;
 9         //根节点入栈
10         stack.push(root);
11         
12         while(!stack.isEmpty()){
13             TreeNode node = stack.pop();
14             res.add(node.val);
15             if(node.right != null) stack.push(node.right);
16             if(node.left != null) stack.push(node.left);
17         }
18         return res;
19     }   
20 }
原文地址:https://www.cnblogs.com/peanut-zh/p/13881584.html