We're given a binary tree and need to find the sum of values of all "left leaves." Two conditions must both be true for a node to count: it must be a leaf (no left or right child), and it must be the left child of its parent.
A node cannot determine on its own whether it is a "left leaf." A leaf node holds no reference to its parent, so it has no way to know whether it is a left child or a right child. That information lives in the parent. The fix is to check from the parent's side: when we visit a node, look at its left child. If that left child has no children of its own, it is a left leaf, so add its value to the sum. We never add the current node's value, only the value of a qualifying left child.
The alternative is to pass a flag like "am I a left child?" down through the recursion. Both work, but checking from the parent avoids carrying that extra state.
Number of nodes in [1, 1000] --> Any standard traversal runs in O(n), which is optimal because every node must be visited at least once to know whether it is a left leaf.-1000 <= Node.val <= 1000 --> Values can be negative, so a left leaf can lower the total. Negative left leaves still count and cannot be skipped.At least 1 node --> The root always exists. A single-node tree returns 0, since the root is not the left child of any parent.A binary tree is defined in terms of itself, so recursion maps onto its structure directly. At each node we evaluate one condition: is the left child a leaf?
A left child is a leaf when it exists and both left.left and left.right are null. When that holds, we add its value. Then we recurse into both subtrees to collect any left leaves deeper in the tree.
The sum only ever counts the value of a qualifying left child, never the current node. Because the check always looks one level down, no "isLeft" flag has to travel through the recursion.
null, return 0.sum = 0.left.val to sum.sum.sum.sum.The recursive approach is optimal in time, but it relies on the call stack, which grows with the tree's height. A deeply skewed tree can exhaust the stack. An iterative traversal removes that risk by managing its own data structure on the heap.
A queue lets us traverse the tree level by level without recursion. The left-leaf check is identical to the recursive version: for each node we dequeue, if its left child has no children, add that child's value to the total.
BFS has a different space profile from DFS. Its memory use scales with the tree's maximum width rather than its height. On a skewed tree, where recursion risks a stack overflow, each level holds a single node, so the queue never exceeds O(1) entries.
root is null, return 0.root to it.sum = 0.left.val to sum.sum.