[ERROR] Binary Tree Preorder Traversal | Help me


  • 0
    D

    Hello all! I have tried make this, because I got error. Could you help me?
    alt text

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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.