It doesn't seem to be a medium level problem


  • 0
    E

    The algorithm is quite straightforward, and I don't think this one should be considered a medium level problem.

    Code in Java:

    public class Solution {
    
    private int sum;
    
    public int sumNumbers(TreeNode root) {
        sum = 0;
        sumNumbers(root, 0);
        return sum;
    }
    
    private void sumNumbers(TreeNode root, int currentValue) {
        if(root==null) return;
        
        int newValue = 10*currentValue + root.val;
        
        if(root.left==null && root.right==null)
            sum += newValue;
        else {
            if(root.left!=null)
                sumNumbers(root.left, newValue);
            if(root.right!=null)
                sumNumbers(root.right, newValue);
        }
    }
    }

Log in to reply
 

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