Skip to content

Binary search tree least common ancestor #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

/**
* Created by divyam on 13/10/16.
* So we will first introduce the problem of least common ancestor of 2 nodes in a binary search tree
* <p>
* Let T be a rooted tree. The lowest common ancestor between two nodes n1 and n2 is defined as the lowest node in T
* that has both n1 and n2 as descendants (where we allow a node to be a descendant of itself)
* for every pair of nodes in a tree we have root node as an ancestor but the problem is to find the lowest common ancestor
* ie we have to find the lca_node such that the sum of distance of the lca_node to the given nodes n1 and n2 is minimum
*/

public class bst_lca {
class Node {
int data;
Node left, right;

Node(int item) {
data = item;
left = right = null;
}
}

Node root;

/* Function to find LCA of n1 and n2. The function assumes that both
n1 and n2 are present in BST */
Node lca(Node node, int n1, int n2) {
if (node == null)
return null;

// If both n1 and n2 are smaller than root, then LCA lies in left
if (node.data > n1 && node.data > n2)
return lca(node.left, n1, n2);

// If both n1 and n2 are greater than root, then LCA lies in right
if (node.data < n1 && node.data < n2)
return lca(node.right, n1, n2);

//the final case when nodes n1 and n2 lie on opposite sides of the present node , we return that node
return node;
}

}