@@ -0,0 +1,1273 @@
+package data_structures.trees;%0D%0A%0D%0A/*%0D%0A * You are given pointer to the root of the binary search tree and two values v1%0D%0A * and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in%0D%0A * the binary search tree. You only need to complete the function.%0D%0A * %0D%0A * Input Format:%0D%0A * %0D%0A * You are given a function,%0D%0A * %0D%0A * node * LCA (node * root ,int v1,int v2) %7B%0D%0A * %0D%0A * %7D%0D%0A * %0D%0A * It is guaranteed that v1 and v2 are present in the tree.%0D%0A * %0D%0A * Node is defined as:%0D%0A * %0D%0A * struct node %7B%0D%0A *     int data;%0D%0A *     node * left;%0D%0A *     node * right;%0D%0A * %7D node;%0D%0A * %0D%0A * Output Format: %0D%0A * Return the LCA of v1 and v2.%0D%0A * %0D%0A * Sample Input: %0D%0A *        4%0D%0A *      /   %5C%0D%0A *     2     7%0D%0A *    / %5C   /%0D%0A *   1   3 6%0D%0A *   %0D%0A * v1=1 and v2=7.%0D%0A * %0D%0A * Sample Output: %0D%0A * LCA of 1 and 7 is 4 (which is the root).%0D%0A * Return a pointer to the root in this case.%0D%0A */%0D%0Apublic class BSTLowestCommonAncestor %7B%0D%0A%0D%0A%09static Node lca(Node root,int v1,int v2) %7B%0D%0A%09    %0D%0A%09    if (root == null) %7B%0D%0A%09        return null;%0D%0A%09    %7D%0D%0A%09    %0D%0A%09    if (root.data %3E v1 && root.data %3E v2) %7B%0D%0A%09        return lca(root.left, v1, v2);%0D%0A%09    %7D else if (root.data %3C v1 && root.data %3C v2) %7B%0D%0A%09        return lca(root.right, v1, v2);%0D%0A%09    %7D else %7B%0D%0A%09        return root;%0D%0A%09    %7D%0D%0A%09%7D%0D%0A%7D

Add BSTLowestCommonAncestor