My Java Solution


  • 1
    public int sumNumbers(TreeNode root) {
        int[] result = new int[1];
        sumNumbersHelper(root, result, 0);
        return result[0];
    }
    
    public void sumNumbersHelper(TreeNode root, int[] result, int count) {
        if(root == null) return;
        
        if(root.left == null && root.right == null) result[0] += 10*count+root.val;
        else {
            sumNumbersHelper(root.left, result, 10*count+root.val);
            sumNumbersHelper(root.right, result, 10*count+root.val);        
        }
    }

Log in to reply
 

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