Given a Binary Search Tree (BST) with unique values and two nodes n1 and n2 (n1 != n2), find their Lowest Common Ancestor (LCA).
You can assume both nodes exist in the tree.
Definition:
The LCA between n1 and n2 is the lowest node in the BST that has both n1 and n2 as descendants (a node can be a descendant of itself).
Input:
root = [5, 4, 6, 3, N, N, 7, N, N, N, 8], n1 = 7, n2 = 8
Output:
7
Explanation:
- The LCA for nodes
7and8is7.
Input:
root = [20, 8, 22, 4, 12, N, N, N, N, 10, 14], n1 = 8, n2 = 14
Output:
8
Explanation:
- The LCA for nodes
8and14is8.
Input:
root = [2, 1, 3], n1 = 1, n2 = 3
Output:
2
Explanation:
- The LCA for nodes
1and3is2.
- The simplest way is to trace paths from the root to both nodes.
- The last common node in both paths is the LCA.
- Traverse the tree from the root to find the path to
n1andn2. - Store each path in a list.
- Compare paths from the root: The last common node in both paths is the LCA.
| Complexity | Value | Description |
|---|---|---|
| Time | O(n) | Visiting each node for both paths |
| Space | O(h) | Stack space for recursion (h = height) + Lists for paths |
1 β€ number of nodes β€ 10β΅1 β€ node->data β€ 10β΅1 β€ n1, n2 β€ 10β΅
/*
class Node
{
int data;
Node left, right;
public Node(int d)
{
data = d;
left = right = null;
}
}
*/
class Solution {
boolean traverse(Node node, Node n, List<Node> list){
if(node == null){
return false;
}
if(node == n){
list.add(node);
return true;
}
if(traverse(node.left, n, list)){
list.add(node);
return true;
}
if(traverse(node.right, n, list)){
list.add(node);
return true;
}
return false;
}
Node LCA(Node root, Node n1, Node n2) {
List<Node> list1 = new ArrayList<>(), list2 = new ArrayList<>();
traverse(root, n1, list1);
traverse(root, n2, list2);
Node ans = root;
int l1 = list1.size() - 1, l2 = list2.size() - 1;
while(l1 >= 0 && l2 >= 0){
if(list1.get(l1) == list2.get(l2)){
ans = list1.get(l1);
} else{
break;
}
l1--;
l2--;
}
return ans;
}
}Made with β€οΈ by Milan Haria


