My short java solution, very easy to understand


  • 0
    H
    private int sum;
    
    public int sumNumbers(TreeNode root) {
        helper(root, 0);
        return sum;
    }
    
    private void helper(TreeNode root, int x) {
        if (root == null) return;
        if (root.left == null && root.right == null) sum += root.val + 10 * x;
        helper(root.left, root.val + 10 * x);
        helper(root.right, root.val + 10 * x);
    }

Log in to reply
 

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