Very short Java recursive


  • 1
    A
    public int sumNumbers(TreeNode root) {
        return getSum(root, 0);
    }
    
    private int getSum(TreeNode root, int sum) {
        if (root == null) return 0;
        int s = sum * 10 + root.val, l = 0, r = 0;
        return (l = getSum(root.left, s)) + (r = getSum(root.right, s)) > 0 ? l + r : s;
    }

Log in to reply
 

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