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.

[ad type=”banner”]

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)

[pastacode lang=”c” manual=”%2F%2F%20A%20recursive%20C%20program%20to%20find%20LCA%20of%20two%20nodes%20n1%20and%20n2.%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20left%2C%20*right%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20find%20LCA%20of%20n1%20and%20n2.%20The%20function%20assumes%20that%20both%0A%20%20%20n1%20and%20n2%20are%20present%20in%20BST%20*%2F%0Astruct%20node%20*lca(struct%20node*%20root%2C%20int%20n1%2C%20int%20n2)%0A%7B%0A%20%20%20%20if%20(root%20%3D%3D%20NULL)%20return%20NULL%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20both%20n1%20and%20n2%20are%20smaller%20than%20root%2C%20then%20LCA%20lies%20in%20left%0A%20%20%20%20if%20(root-%3Edata%20%3E%20n1%20%26%26%20root-%3Edata%20%3E%20n2)%0A%20%20%20%20%20%20%20%20return%20lca(root-%3Eleft%2C%20n1%2C%20n2)%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20both%20n1%20and%20n2%20are%20greater%20than%20root%2C%20then%20LCA%20lies%20in%20right%0A%20%20%20%20if%20(root-%3Edata%20%3C%20n1%20%26%26%20root-%3Edata%20%3C%20n2)%0A%20%20%20%20%20%20%20%20return%20lca(root-%3Eright%2C%20n1%2C%20n2)%3B%0A%20%0A%20%20%20%20return%20root%3B%0A%7D%0A%20%0A%2F*%20Helper%20function%20that%20allocates%20a%20new%20node%20with%20the%20given%20data.*%2F%0Astruct%20node*%20newNode(int%20data)%0A%7B%0A%20%20%20%20struct%20node*%20node%20%3D%20(struct%20node*)malloc(sizeof(struct%20node))%3B%0A%20%20%20%20node-%3Edata%20%20%3D%20data%3B%0A%20%20%20%20node-%3Eleft%20%20%3D%20node-%3Eright%20%3D%20NULL%3B%0A%20%20%20%20return(node)%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20lca()%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Let%20us%20construct%20the%20BST%20shown%20in%20the%20above%20figure%0A%20%20%20%20struct%20node%20*root%20%20%20%20%20%20%20%20%3D%20newNode(20)%3B%0A%20%20%20%20root-%3Eleft%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20newNode(8)%3B%0A%20%20%20%20root-%3Eright%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20newNode(22)%3B%0A%20%20%20%20root-%3Eleft-%3Eleft%20%20%20%20%20%20%20%20%20%3D%20newNode(4)%3B%0A%20%20%20%20root-%3Eleft-%3Eright%20%20%20%20%20%20%20%20%3D%20newNode(12)%3B%0A%20%20%20%20root-%3Eleft-%3Eright-%3Eleft%20%20%3D%20newNode(10)%3B%0A%20%20%20%20root-%3Eleft-%3Eright-%3Eright%20%3D%20newNode(14)%3B%0A%20%0A%20%20%20%20int%20n1%20%3D%2010%2C%20n2%20%3D%2014%3B%0A%20%20%20%20struct%20node%20*t%20%3D%20lca(root%2C%20n1%2C%20n2)%3B%0A%20%20%20%20printf(%22LCA%20of%20%25d%20and%20%25d%20is%20%25d%20%5Cn%22%2C%20n1%2C%20n2%2C%20t-%3Edata)%3B%0A%20%0A%20%20%20%20n1%20%3D%2014%2C%20n2%20%3D%208%3B%0A%20%20%20%20t%20%3D%20lca(root%2C%20n1%2C%20n2)%3B%0A%20%20%20%20printf(%22LCA%20of%20%25d%20and%20%25d%20is%20%25d%20%5Cn%22%2C%20n1%2C%20n2%2C%20t-%3Edata)%3B%0A%20%0A%20%20%20%20n1%20%3D%2010%2C%20n2%20%3D%2022%3B%0A%20%20%20%20t%20%3D%20lca(root%2C%20n1%2C%20n2)%3B%0A%20%20%20%20printf(%22LCA%20of%20%25d%20and%20%25d%20is%20%25d%20%5Cn%22%2C%20n1%2C%20n2%2C%20t-%3Edata)%3B%0A%20%0A%20%20%20%20getchar()%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

LCA of 10 and 14 is 12
LCA of 14 and 8 is 8
LCA of 10 and 22 is 20
[ad type=”banner”]

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.

[pastacode lang=”c” manual=”%2F*%20Function%20to%20find%20LCA%20of%20n1%20and%20n2.%20The%20function%20assumes%20that%20both%0A%20%20%20n1%20and%20n2%20are%20present%20in%20BST%20*%2F%0Astruct%20node%20*lca(struct%20node*%20root%2C%20int%20n1%2C%20int%20n2)%0A%7B%0A%20%20%20%20while%20(root%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%2F%2F%20If%20both%20n1%20and%20n2%20are%20smaller%20than%20root%2C%20then%20LCA%20lies%20in%20left%0A%20%20%20%20%20%20%20%20if%20(root-%3Edata%20%3E%20n1%20%26%26%20root-%3Edata%20%3E%20n2)%0A%20%20%20%20%20%20%20%20%20%20%20root%20%3D%20root-%3Eleft%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20both%20n1%20and%20n2%20are%20greater%20than%20root%2C%20then%20LCA%20lies%20in%20right%0A%20%20%20%20%20%20%20%20else%20if%20(root-%3Edata%20%3C%20n1%20%26%26%20root-%3Edata%20%3C%20n2)%0A%20%20%20%20%20%20%20%20%20%20%20root%20%3D%20root-%3Eright%3B%0A%20%0A%20%20%20%20%20%20%20%20else%20break%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20root%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]