A Java Solution (Vanilla but Short)


  • 0

    public class Solution {

    int res = 0; 
    public int sumNumbers(TreeNode root) {
        if (root != null) sumUtil(root, ""); 
        return res; 
    }
    
    private void sumUtil(TreeNode root, String curr){
        if (root.left == null && root.right == null) {curr += root.val; res += Integer.parseInt(curr); return;}
        if (root.left != null) sumUtil(root.left, curr + root.val); 
        if (root.right != null) sumUtil(root.right, curr + root.val); 
    }
    

    }


Log in to reply
 

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