[JAVA] Recursive with left check / T : O(N), S : O(1) or O(logN)


  • 0
    J
    class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            return sum(root, false);
        }
        
        public int sum(TreeNode root, boolean left){
            if(root == null)
                return 0;
            if(root.left == null && root.right == null)
                return left ? root.val : 0;
            
            
            return sum(root.left, true) + sum(root.right, false); 
        }
    }
    

Log in to reply
 

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