[ERROR] Binary Tree Preorder Traversal | Help me

  • 0

    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>();
            while (!stack.isEmpty()) {
                root = stack.pop();
                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.