Javascript Easy to Understand Recursive Solution

  • 0

    If the root is null return 0.
    Else, if the root's left child is a leaf, return that value added to the recurse call on the root's right child.
    Otherwise, the root's left child is not a leaf so we just push the problem to the left and right sub trees. Return the sum of the recurse call on the root's left and right children.

    var sumOfLeftLeaves = function(root) {
        if (root === null) {
            return 0;
        } else if (root.left !== null && root.left.left === null && root.left.right === null) {
            return root.left.val + sumOfLeftLeaves(root.right);
        return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);

Log in to reply

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