首页 > 代码库 > [LeetCode] Binary Tree Preorder Traversal

[LeetCode] Binary Tree Preorder Traversal

public class Solution {    public List<Integer> preorderTraversal(TreeNode root) {         List<Integer> result = new ArrayList<Integer>();        Stack<TreeNode> nodeStack = new Stack<TreeNode>();        TreeNode nowNode = root;                while (!nodeStack.isEmpty() || nowNode != null) {            if (nowNode != null) {                result.add(nowNode.val);                nodeStack.push(nowNode.right);                nodeStack.push(nowNode.left);                            }            nowNode = nodeStack.pop();        }        return result;    }}

 

[LeetCode] Binary Tree Preorder Traversal