Javascript recursive solution, if anyone is interested


  • 0
    P
    var sumOfLeftLeaves = function(root) {
        var sum = 0;
        function rec(node, isLeft) {
            if (node) {
                // Is a left leaf
                if (isLeft && !node.left && !node.right) 
                    sum += node.val;
                // Keep traveling down
                if (node.right)
                    rec(node.right, false);
                if (node.left)
                    rec(node.left, true);
            }
        }
        rec(root, false);
        
        return sum;
    };
    

Log in to reply
 

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