464. Left Leaf Total
A knockout bracket is stored as a binary tree rooted at root; each node carries the score of a match. The organiser wants a quick audit figure: the sum of the scores of all left leaves. A left leaf is a node with no children that is the left child of its parent.
Return that sum. A leaf that is a right child does not count, a node that has any child is not a leaf, and the root is never counted as a left leaf, even when it stands alone. An empty tree gives 0.
Example 1
- Input:
- root = [10,4,7,3,6,5,2,1]
- Output:
- 6
- Explanation:
The left leaves are 1 (left child of 3) and 5 (left child of 7); 6 and 2 are right children, so the total is 6.
Example 2
- Input:
- root = [8,null,5,-3]
- Output:
- -3
- Explanation:
The only left leaf is -3, the left child of 5; the root is never counted, so the total is -3.
Constraints
0 ≤ number of nodes in root ≤ 1000
-1000 ≤ Node.val ≤ 1000
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n)
- Space
- O(h)