1676. Lowest Common Ancestor of a Binary Tree IV
Asked at Atlassian
Difficulty
Medium
Time
O(n)
Space
O(n)
Pattern
Hash Table
Companies
1
Problem
You are given the root of a binary tree and a list of nodes called nodes. Every node in that list really exists in the tree, and every value in the tree is unique.
Return the lowest common ancestor (LCA) of all the nodes in the list. A node x is an ancestor of a node y if y sits on the path going down from x (a node counts as an ancestor of itself). The LCA is the deepest node in the tree that is an ancestor of every node in the list.
If the list has only one node, the answer is that node itself. The answer is always a single node from the tree, so return the node object, not its value.
Find the deepest node covering all targets
Constraints
- ●
The number of nodes in the tree is in the range [1, 10^4]. - ●
-10^9 <= Node.val <= 10^9 - ●
All Node.val are unique. - ●
All nodes[i] will exist in the tree. - ●
All nodes[i] are distinct.
Examples
root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [4,7]2root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [1]1root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [7,6,2,4]5Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Atlassian | Medium | View all Atlassian questions → |
How to Think About It
Brute force: check every node and verify if all target nodes exist in its subtree.
Use a hash set to store target nodes and return early when a subtree contains them.
Post-order traversal: recurse left and right, merging results upward.
If both left and right return non-null, current node is the LCA.
Optimal: single pass O(n) DFS with a set of targets, returning null if subtree contains zero targets.
Optimal Approach
Perform a depth-first search on the tree. Maintain a set of target nodes. At each node, recurse left and right. If the current node is a target, return it. If both left and right return non-null values, the current node is the LCA. If only one side returns non-null, propagate that result upward. If the current subtree contains fewer targets than the total, return null to signal upward that this subtree is irrelevant. This runs in O(n) time where n is the number of nodes.
Post-order DFS returns targets upward
What Trips People Up in Real Interviews
Clarify whether a node can be its own ancestor (yes in this variant).
Ask if all nodes are guaranteed to exist in the tree.
Mention that a hash set avoids repeated searches for each target node.
Explain the post-order pattern: process children before the current node.
Discuss how this differs from the classic two-node LCA problem.
Solution Code
O(n) timeclass Solution:
def lowestCommonAncestor(self, root: 'TreeNode', nodes: 'List[TreeNode]') -> 'TreeNode':
targets = set(nodes)
def dfs(node):
if not node:
return None
if node in targets:
return node
left = dfs(node.left)
right = dfs(node.right)
if left and right:
return node
return left if left else right
return dfs(root)Frequently Asked Questions
What is the Lowest Common Ancestor of a Binary Tree IV problem?
Given a binary tree and a list of nodes, find the lowest common ancestor of all the given nodes. The LCA is the deepest node that is an ancestor of every node in the list, where a node can be an ancestor of itself.
How do you solve Lowest Common Ancestor of a Binary Tree IV?
The optimal approach runs in O(n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Lowest Common Ancestor of a Binary Tree IV?
Lowest Common Ancestor of a Binary Tree IV is asked at Atlassian. It is a medium difficulty problem.
What are common mistakes on Lowest Common Ancestor of a Binary Tree IV?
- Clarify whether a node can be its own ancestor (yes in this variant).
- Ask if all nodes are guaranteed to exist in the tree.
- Mention that a hash set avoids repeated searches for each target node.
- Explain the post-order pattern: process children before the current node.
- Discuss how this differs from the classic two-node LCA problem.