366. Find Leaves of Binary Tree

Find Leaves of Binary Tree (LeetCode 366) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Postorder Traversal subpattern.

How to solve Find Leaves of Binary Tree

Find Leaves of Binary Tree is a Postorder Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master postorder traversal for tree height, diameter, and path sum problems. Process children before root.

Work towards the solution in these steps:

  1. Consider how traversing the tree can help identify and collect leaf nodes efficiently in each iteration.
  2. Think about using a postorder traversal (left, right, root) to determine when a node becomes a leaf after its children are processed.
  3. How can you assign a 'height' or 'depth' value to each node that corresponds to the round it will be removed as a leaf?

The reference solution runs in O(N) time and O(N) space.

The Find Leaves of Binary Tree solution and editorial walks through the full approach with reference code.

Problem statement

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.

Examples

Example 1

Input: {"root": {"val": 1, "left": {"val": 2, "left": {"val": 4, "left": null, "right": null}, "right": {"val": 5, "left": null, "right": null}}, "right": {"val": 3, "left": null, "right": null}}}

Output: [[4, 5, 3], [2], [1]]

Explanation: Collects leaves layer by layer from a balanced binary tree.

Example 2

Input: {"root": {"val": 1, "left": null, "right": {"val": 2, "left": null, "right": {"val": 3, "left": null, "right": null}}}}

Output: [[3], [2], [1]]

Explanation: Handles a right-skewed tree, collecting leaves from bottom up.

Topics and companies

Topics: Tree, Depth-First Search, Binary Tree.

Reported in interviews at LinkedIn, Amazon.

How to practise Find Leaves of Binary Tree on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N) time and O(N) space.

Open Find Leaves of Binary Tree in the code editor, or read the Find Leaves of Binary Tree editorial for a worked solution with its approach and complexity analysis.

Where Find Leaves of Binary Tree sits in the DSA pattern sheet

Problems related to Find Leaves of Binary Tree

Other problems that use the same Recursive Postorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:

Find Leaves of Binary Tree FAQ

What is the LeetCode number of Find Leaves of Binary Tree?

Find Leaves of Binary Tree is LeetCode problem 366, rated Medium.

What pattern is used to solve Find Leaves of Binary Tree?

Find Leaves of Binary Tree is a Postorder Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Postorder Traversal subpattern.

What is the time complexity of Find Leaves of Binary Tree?

The reference solution runs in O(N) time and O(N) space.

Which companies ask Find Leaves of Binary Tree?

Find Leaves of Binary Tree has been reported in interviews at LinkedIn, Amazon.

Preparing this workspace.