Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Lowest Common Ancestor

DZone's Guide to

Lowest Common Ancestor

·
Free Resource

public static Node getLCANode(Node root, int key1, int key2) {
if (root == null) {
return null;
} 
if (key1 > root.key && key2 > root.key) {
return getLCANode(root.rightNode,key1,key2);
}
if (key1 < root.key && key2 < root.key) {
return getLCANode(root.leftNode,key1,key2);
} else {
return root;	
}
}

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}