Lowest Common Ancestor of a Binary Tree IV - Problem
Lowest Common Ancestor of Multiple Nodes
You're given the
Think of it as finding the "meeting point" in a family tree - the closest common ancestor that connects all the specified family members. Unlike the classic LCA problem that deals with just two nodes, this version challenges you to handle multiple nodes simultaneously.
Key Points:
• All nodes in the array are guaranteed to exist in the tree
• All node values are unique
• A node can be considered its own descendant
• The LCA is the deepest node that has all target nodes as descendants
Goal: Return the TreeNode representing the lowest common ancestor of all nodes in the input array.
You're given the
root of a binary tree and an array of TreeNode objects called nodes. Your mission is to find the lowest common ancestor (LCA) of all the nodes in the array.Think of it as finding the "meeting point" in a family tree - the closest common ancestor that connects all the specified family members. Unlike the classic LCA problem that deals with just two nodes, this version challenges you to handle multiple nodes simultaneously.
Key Points:
• All nodes in the array are guaranteed to exist in the tree
• All node values are unique
• A node can be considered its own descendant
• The LCA is the deepest node that has all target nodes as descendants
Goal: Return the TreeNode representing the lowest common ancestor of all nodes in the input array.
Input & Output
example_1.py — Basic Case
$
Input:
root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [4,7,2]
›
Output:
2
💡 Note:
The LCA of nodes 4, 7, and 2 is node 2. Node 2 is the deepest node that has all three target nodes as descendants (4 and 7 are in its subtree, and 2 is itself).
example_2.py — Root as LCA
$
Input:
root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [1,6,8]
›
Output:
3
💡 Note:
Node 6 is in the left subtree of 3, while nodes 1 and 8 are in the right subtree of 3. The only common ancestor that contains all three nodes is the root node 3.
example_3.py — Single Node
$
Input:
root = [1,2], nodes = [2]
›
Output:
2
💡 Note:
When there's only one target node, the LCA is the node itself. Node 2 is its own lowest common ancestor.
Constraints
- The number of nodes in the tree is in the range [1, 104]
- -109 ≤ Node.val ≤ 109
- All Node.val are unique
- All nodes[i] will exist in the tree
- All nodes[i] are unique
- 1 ≤ nodes.length ≤ 104
Visualization
Tap to expand
Understanding the Visualization
1
Identify Family Members
Create a list of all family members who want to attend the reunion (target nodes)
2
Trace Family Lines
Starting from the youngest generation, trace upward through the family tree
3
Find Meeting Point
The first ancestor whose house can accommodate all family members is the optimal meeting location
4
Confirm Accessibility
Verify that all target family members can trace their lineage to this ancestor
Key Takeaway
🎯 Key Insight: The LCA is like finding the perfect family reunion location - the closest common ancestor that all target family members can reach, representing the optimal meeting point in the tree hierarchy.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code