Skip to content

Practice Smallest Subtree with all the Deepest Nodes

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#865
LeetCode Problem

Smallest Subtree with all the Deepest Nodes

Problem Statement

Return the root of the smallest subtree containing every deepest node of the given binary tree. Depth is the number of edges from the root. A subtree contains its own root and all that node's descendants. This is equivalent to finding the lowest common ancestor of the deepest leaves.

Source: Smallest Subtree with all the Deepest Nodes on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [3,5,1,6,2,0,8,null,null,7,4]
Output
[2,7,4]

# Example 2

Input
root = [1]
Output
[1]

# Example 3

Input
root = [0,1,3,null,2]
Output
[2]

Constraints

The number of nodes in the tree will be in the range [1, 500].
0 <= Node.val <= 500
The values of the nodes in the tree are unique.

Related LeetCode Problems

Practice more problems similar to Smallest Subtree with all the Deepest Nodes.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.