0ms Java Recursive Solution


  • 0
    Y
    public int sumNumbers(TreeNode root) {
        return leafNumbers(root,0);
    }
    public int leafNumbers(TreeNode node,int sum){
        if(node==null) return 0;
        int temp = sum*10+node.val;
        if(node.left==null&&node.right==null) return temp;
        return leafNumbers(node.left,temp)+leafNumbers(node.right,temp);
    }

Log in to reply
 

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