Skip to content

1123. Lowest Common Ancestor of Deepest Leaves #1517

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the lowest common ancestor (LCA) of the deepest leaves in a binary tree. The LCA of a set of nodes is the node with the largest depth such that every node in the set is in the subtree rooted at this node. The deepest leaves are the leaves at the maximum depth in the tree.

Approach

The approach involves a post-order traversal of the tree. For each node, we compute the maximum depth of its left and right subtrees. If both subtrees have the same maximum depth, the current node is the LCA of the deepest leaves in its subtree. Otherwise, the LCA is derived from the subtree with the greater depth. This approach ensures that we efficiently determine the LCA without explicitly col…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Apr 4, 2025
Maintainer Author

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Apr 4, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Apr 4, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants