The Find Leaves of Binary Tree interview question asks you to collect and remove the leaf nodes of a binary tree in several "rounds." In each round, you find all the current leaf nodes, record their values, and then detach them from the tree. You repeat this until the tree is empty. The output should be a list of lists, where each sub-list contains the leaves removed in a specific round.
Companies like LinkedIn and Amazon use this to test your understanding of Tree Traversal and node height. The key insight is that a node's "round" of removal corresponds to its distance from the bottom of the tree. It evaluations your proficiency with the Depth-First Search (DFS) interview pattern and your ability to use recursive return values to determine a node's position relative to its descendants.
This problem is elegantly solved using DFS based on Height.
1 + max(height(left), height(right)).Tree:
1
/
2 3
/
4 5
[4, 5, 3].[2].[1].
Result: [[4, 5, 3], [2], [1]].Height is the distance to the farthest leaf. Depth is the distance from the root. Recognizing when to use height vs depth is a fundamental part of the Binary Tree interview pattern. For bottom-up problems, always think about the height.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Binary Tree Coloring Game | Medium | Solve | |
| Delete Leaves With a Given Value | Medium | Solve | |
| Flip Equivalent Binary Trees | Medium | Solve | |
| Path Sum III | Medium | Solve | |
| Binary Tree Longest Consecutive Sequence | Medium | Solve |