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: 1 with children 2, 3; 2 has right child 5; 3 has right child 4 → Output: [1, 2, 5] (level 2's leftmost is 5 even though it's a right child)
asked …
LeaderboardSalaryAccount