Leftmost Node at Each Tree Level
Problem Given a binary tree, print the leftmost node at each level (the first node visible when the tree is viewed from the left at each depth).
Input / Output
- Input: root of a binary tree
- Output: list of values, one per level, top to bottom — the leftmost node of each level
Constraints
- Arbitrary depth and shape; tree may be skewed
- Up to ~10^5 nodes; O(n) time expected
Example
- Tree:
1with children2, 3;2has right child5;3has right child4→ Output:[1, 2, 5](level 2's leftmost is 5 even though it's a right child)
asked …