My 0ms recursive solution Java


  • 0
    A

    public class Solution {
    public int sumNumbers(TreeNode root) {
    if(root == null) {
    return 0;
    }
    return sumNumbers(root,0);
    }

    public static int sumNumbers(TreeNode root, int sum) {
        if(root == null) {
            return 0;
        }
        return Math.max(sum*10 + root.val,sumNumbers(root.left,sum*10+root.val)+sumNumbers(root.right,sum*10+root.val));
    }
    

    }


Log in to reply
 

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