Javascript Recursive Solution (DFS)


  • 0
    G
    var sumOfLeftLeaves = function(root) {
        var sum = 0;
        
        function traverse(node) {
            if (!node) {
                return;
            }
            
            traverse(node.right);  
            
            if (node.left && !node.left.left && !node.left.right) {
                sum += node.left.val;
            }
            
            traverse(node.left);
        }
        
        traverse(root);
        
        return sum;
    };
    

Log in to reply
 

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