Accepted Javascript recursive solution


  • 0
    R

    Any comments on improvement welcomed!

    function sumOfLeftLeaves(root, sum = 0, isLeft = false) {
        if (!root) {
            return sum;
        }
    
        sum = sumOfLeftLeaves(root.left, sum, true);
    
        if (isLeft && !root.left && !root.right) {
            sum += root.val;
        }
    
        sum = sumOfLeftLeaves(root.right, sum, false);
    
        return sum;
    }
    

Log in to reply
 

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