Simple recusion


  • 0
    A
        static int total = 0;
        public int sumNumbers(TreeNode root) {
            total = 0 ;
            pathSum(root,0);
            return total;
        }
        
        public void pathSum(TreeNode root,int sum){
            if (root == null) return;
            int pathsum = (sum*10) + root.val;
            System.out.print(pathsum);
            if(root.left == null && root.right == null) {
                total+= pathsum;
                return;
            }
            
            pathSum(root.left,pathsum);
            pathSum(root.right,pathsum);
        } 
        
    }```

Log in to reply
 

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