Part 14 of 15 in Binary Search Trees  

Lowest Common Ancestor in a Binary Search Tree by phantom11

Problem statement

Write a function to find the Lowest Common Ancestor(LCA) of two nodes in a binary search tree. A binary search tree is a binary tree in which value of all the nodes left of a node are lesser than it and value of all the nodes right of node are greater than it. You may assume that there are no duplicate values in the tree. The LCA of two nodes in a BST is defined as the deepest node from the root which lies in the path of both the nodes from the root.


You need to write a function named "LCAInBST" that will take root of a binary search tree and two data A and B, whose LCA needs to be found as arguments. You can assume that both these data are present in the tree and are different from each other.


Your function must return pointer to LCA node.

To try out your code

Sign in

Sign up