Ctrl K

Left-Hand Leaves Total

Easy
You are given the root of a binary tree. A leaf is a node with no children, and a left leaf is a leaf that is the left child of some node. Return the sum of the values of all left leaves, or 0 if there are none.
Example 1
root=tree_node([6, 2, 8, None, 4, 7, 1]) → 7

The leaves are 4, 7 and 1. Only 7 is a left child (of 8); 4 is the right child of 2 and 1 is the right child of 8. The answer is 7.

Example 2
root=tree_node([-3, -5, 2, None, None, 9]) → 4

The left leaves are -5 (left of the root) and 9 (left of 2), summing to 4.

Example 3
root=tree_node([10]) → 0

The root is a leaf, but it is nobody's child, so it does not count. The answer is 0.

Constraints
  • 1 ≤ number of nodes ≤ 1000
  • -1000 ≤ Node.val ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIdentical Trees EasyMirror-Image Tree EasyTree Height EasyNearest Leaf Depth EasyLeaf Path With a Given Total Easy
loading Python…
Input
root=tree_node([6, 2, 8, None, 4, 7, 1])
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 97 tests on the server, hidden ones included.