Lowest Common Ancestor of a Binary Tree IV - Problem

Lowest Common Ancestor of Multiple Nodes

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
Lowest Common Ancestor of Binary Tree IV INPUT Binary Tree: 3 5 1 6 2 0 8 7 4 Target Nodes (orange): 4 7 2 nodes = [4, 7, 2] ALGORITHM (Hash) 1 Create HashSet Store target nodes: {4,7,2} {4, 7, 2} 2 DFS Traversal Post-order: left, right, node 3 Check Each Node If node in HashSet or subtree has target: return Visit 7 --> in set --> return 7 Visit 4 --> in set --> return 4 Visit 2 --> both children returned --> LCA = 2 4 Find LCA Node where paths converge FINAL RESULT LCA Found at Node 2: 2 LCA 7 4 Output: 2 [OK] Node 2 is ancestor of all targets: 4, 7, 2 (Node can be its own ancestor) Key Insight: Use a HashSet for O(1) lookup of target nodes. In post-order DFS, when a node is in the set OR both subtrees return non-null, that node is the LCA. Time: O(n), Space: O(n) for recursion + HashSet. TutorialsPoint - Lowest Common Ancestor of a Binary Tree IV | Hash Approach
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.3K Views
High Frequency
~18 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen