Panda Guru LogoPanda
Guru

Find Leaves of Binary Tree

Round 1

Questions: Given the root of a binary tree, collect and remove all of its leaves in a top-down manner, and repeat the process until the tree becomes empty. You should return a list of lists where:

Example 1
Input:

1 / \ 2 3 / \ \ 4 5 6

Output:

[[4,5,6],[2,3],[1]]

Explanation:

  1. First round: Remove leaves [4,5,6], remaining tree:
1 / \ 2 3
  1. Second round: Remove leaves [2,3], remaining tree:
    1
  1. Third round: Remove root [1], tree is now empty.

Example 2
Input:

1 / 2 / 3

Output:

[[3],[2],[1]]

Constraints

Follow-up
Can you solve this problem in O(n) time complexity?

Candidate's Approach

No approach provided.

Interviewer's Feedback

No feedback provided.