Leetcode 404 - Sum of left leaves

Note

  • Very typical recursion problem.
  • The key is to check if cur node’s left subtree is a leaf node.

Given the root of a binary tree, return the sum of all left leaves.

Example

img

1
2
3
Input: root = [3,9,20,null,null,15,7]
Output: 24
Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var sumOfLeftLeaves = function(root) {
let result = 0;
result = recursion(root);
return result;

function recursion(node) {
if (!node) return 0;
if (node.left && !node.left.left && !node.left.right) {
return node.left.val + recursion(node.left) + recursion(node.right);
}
return recursion(node.left) + recursion(node.right);
}
};