Skip to content

Commit 624bda5

Browse files
authored
Add files via upload
1 parent f75fbd8 commit 624bda5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Trees/LowestCommonAncestor.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Binary Search Tree : Lowest Common Ancestor
3+
4+
Node is defined as :
5+
class Node
6+
int data;
7+
Node left;
8+
Node right;
9+
10+
*/
11+
12+
13+
static Node lca(Node root,int v1, int v2)
14+
{
15+
if(root==null)
16+
return null;
17+
18+
if(root.data==v1 || root.data==v2)
19+
return root;
20+
21+
Node left = lca(root.left,v1,v2);
22+
Node right = lca(root.right,v1,v2);
23+
24+
if(left!=null && right !=null)
25+
return root;
26+
27+
return (left==null)?right:left;
28+
}
29+
30+
31+
32+

0 commit comments

Comments
 (0)