Help!! Why is my solution error?


  • 0
    J

    Please help! Thank you!

        static int sum;
        public int sumNumbers(TreeNode root) {
            if(root == null) return 0; 
            helper(root,0);
            return this.sum;
        }
        public void helper(TreeNode node, int n){
            n = n * 10 + node.val;
            if(node.left == null && node.right == null){
                this.sum = this.sum + n;
                return;
            }
            if(node.left != null) helper(node.left,n);
            if(node.right != null) helper(node.right,n);
        }
    
    Input:[0,1]
    Output:10
    Expected:1

Log in to reply
 

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