Sum of Left Leaves
LeetCode August - Day 24
Problem Statement
Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
Inputs, Outputs, Constraints & Exceptions
- I: TreeNode(val, left, right) {root}
- O: number
- C: none/optimise
- E: - root is null
- root is the only node
- all child nodes are
.rightnodes
Code (inorder traversal)
/**
* 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}
*/
function sumOfLeftLeaves(root) {
let sum = 0;
const traverse = (node, isLeftLeaf = false) => {
if(!node) return;
if(isLeftLeaf && !node.left && !node.right){
sum += node.val
}
traverse(node.left, true)
traverse(node.right)
}
traverse(root)
return sum
}
Time complexity: O(N)
Space complexity: O(N)