Problem 2: Lowest Common Ancestor of a Binary Search Tree (34)
Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia, the lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (a node can be a descendant of itself).
For example, given the following binary search tree:
The LCA of nodes 2 and 8 is 6.
The LCA of nodes 4 and 7 is 6.
The LCA of nodes 2 and 3 is 2.
The LCA of nodes 7 and 9 is 8.
If either of the two nodes p or q does not exist in the BST, you will also need to display an error message.
Outputs:
The LCA of nodes 2 and 8 is 6.
The LCA of nodes 0 and 3 is 2.
There is no such node 10 in the tree.
There is no such node 1 in the tree.