1ms Java Recursive Solution


  • 0
    J

    '''
    public class Solution {
    int totSum;
    public int sumNumbers(TreeNode root) {
    totSum=0;

        if(root==null) return totSum;
        helper(root,totSum);
        
        return totSum;
    }
    
    public void helper(TreeNode root,int curSum)
    {
        int mySum=curSum*10 + root.val;
        
        if(root.left==null && root.right==null){totSum+=mySum; return;}
        
        if(root.left!=null) helper(root.left,mySum);
        if(root.right!=null) helper(root.right,mySum);
    }
    

    }
    '''


Log in to reply
 

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