Lowest Common Ancestor in a Binary Search Tree.(在二叉搜索树中寻找两个节点的最近公共祖先)
Given values of two nodes in a Binary Search Tree, write a c program to find the Lowest Common Ancestor (LCA). You may assume that both the values exist in the tree.
The function prototype should be as follows:
struct node *lca(node* root, int n1, int n2)
n1 and n2 are two given values in the tree with given root.
For example, consider the BST in diagram, LCA of 10 and 14 is 12 and LCA of 8 and 14 is 8.
Following is definition of LCA from Wikipedia: Let T be a rooted tree. The lowest common ancestor between two nodes n1 and n2 is defined as the lowest node in T that has both n1 and n2 as descendants (where we allow a node to be a descendant of itself).
The LCA of n1 and n2 in T is the shared ancestor of n1 and n2 that is located farthest from the root. Computation of lowest common ancestors may be useful, for instance, as part of a procedure for determining the distance between pairs of nodes in a tree: the distance from n1 to n2 can be computed as the distance from the root to n1, plus the distance from the root to n2, minus twice the distance from the root to their lowest common ancestor. (Source Wiki)
Solutions: If we are given a BST where every node has parent pointer, then LCA can be easily determined by traversing up using parent pointer and printing the first intersecting node.
We can solve this problem using BST properties. We can recursively traverse the BST from root. The main idea of the solution is, while traversing from top to bottom, the first node n we encounter with value between n1 and n2, i.e., n1 < n < n2 or same as one of the n1 or n2, is LCA of n1 and n2 (assuming that n1 < n2). So just recursively traverse the BST in, if node’s value is greater than both n1 and n2 then our LCA lies in left side of the node, if it’s is smaller than both n1 and n2, then LCA lies on right side. Otherwise root is LCA (assuming that both n1 and n2 are present in BST)
1 // A recursive C program to find LCA of two nodes n1 and n2. 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 struct node 6 { 7 int data; 8 struct node* left, *right; 9 }; 10 11 /* Function to find LCA of n1 and n2. The function assumes that both 12 n1 and n2 are present in BST */ 13 struct node *lca(struct node* root, int n1, int n2) 14 { 15 if (root == NULL) return NULL; 16 17 // If both n1 and n2 are smaller than root, then LCA lies in left 18 if (root->data > n1 && root->data > n2) 19 return lca(root->left, n1, n2); 20 21 // If both n1 and n2 are greater than root, then LCA lies in right 22 if (root->data < n1 && root->data < n2) 23 return lca(root->right, n1, n2); 24 25 return root; 26 } 27 28 /* Helper function that allocates a new node with the given data.*/ 29 struct node* newNode(int data) 30 { 31 struct node* node = (struct node*)malloc(sizeof(struct node)); 32 node->data = data; 33 node->left = node->right = NULL; 34 return(node); 35 } 36 37 /* Driver program to test lca() */ 38 int main() 39 { 40 // Let us construct the BST shown in the above figure 41 struct node *root = newNode(20); 42 root->left = newNode(8); 43 root->right = newNode(22); 44 root->left->left = newNode(4); 45 root->left->right = newNode(12); 46 root->left->right->left = newNode(10); 47 root->left->right->right = newNode(14); 48 49 int n1 = 10, n2 = 14; 50 struct node *t = lca(root, n1, n2); 51 printf("LCA of %d and %d is %d \n", n1, n2, t->data); 52 53 n1 = 14, n2 = 8; 54 t = lca(root, n1, n2); 55 printf("LCA of %d and %d is %d \n", n1, n2, t->data); 56 57 n1 = 10, n2 = 22; 58 t = lca(root, n1, n2); 59 printf("LCA of %d and %d is %d \n", n1, n2, t->data); 60 61 getchar(); 62 return 0; 63 }
Output:
LCA of 10 and 14 is 12 LCA of 14 and 8 is 8 LCA of 10 and 22 is 20
Time complexity of above solution is O(h) where h is height of tree. Also, the above solution requires O(h) extra
space in function call stack for recursive function calls. We can avoid extra space using iterative solution.
1 /* Function to find LCA of n1 and n2. The function assumes that both 2 n1 and n2 are present in BST */ 3 struct node *lca(struct node* root, int n1, int n2) 4 { 5 while (root != NULL) 6 { 7 // If both n1 and n2 are smaller than root, then LCA lies in left 8 if (root->data > n1 && root->data > n2) 9 root = root->left; 10 11 // If both n1 and n2 are greater than root, then LCA lies in right 12 else if (root->data < n1 && root->data < n2) 13 root = root->right; 14 15 else break; 16 } 17 return root; 18 }
Exercise
The above functions assume that n1 and n2 both are in BST. If n1 and n2 are not present, then they may return incorrect result. Extend the above solutions to return NULL if n1 or n2 or both not present in BST.