1ms Java recursion


  • 0
    public int sumNumbers(TreeNode root) {
            return calculate(root,0,0);
            
        }
         public int calculate(TreeNode root, int level,int sum) {
             if(root==null) return 0;
             sum= level==0? root.val:sum*10+root.val;
             if(root.left==null&&root.right==null) return sum;
             else 
             return calculate(root.left,level+1,sum)+calculate(root.right,level+1,sum);
             
         }
    
    

Log in to reply
 

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