Simple, 0ms Java, Beats 73%


  • 1
    T
    public class Solution {
        public int sumNumbers(TreeNode root) {
            return helper(root, 0);   
        }
        
        public int helper(TreeNode root, int sum){
            if(root == null) return 0;
            
            sum = sum*10 + root.val;
            if(root.left == null && root.right == null)
                return sum;
    
            return helper(root.left, sum) + helper(root.right, sum);
        }
    }
    

Log in to reply
 

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