Skip to main content

Find Leaves of Binary Tree

Medium
Data Structures and Algorithms
Editorial
Topics:
Tree
Depth-First Search
Binary Tree

You are given the root of a binary tree. Repeatedly remove all leaf nodes (nodes with no children) from the tree, and collect the values of these nodes in a list for each round of removal. Continue this process until the tree is empty. Return a list of lists, where each inner list contains the values of the leaves removed at each round, in the order they were removed.

Constraints:

  • The number of nodes in the tree is in the range [1, 100].
  • Each node's value is a unique integer.

Return the list of lists of node values, where each inner list represents the leaves removed in each round.

Need help or want more practice?

Practise in an interviewGet AI Coach help