Created
April 24, 2014 07:11
-
-
Save gabhi/11244538 to your computer and use it in GitHub Desktop.
lca lowest common ancestor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct node *lca(struct node* root, int n1, int n2) | |
{ | |
if (root == NULL) return NULL; | |
// If both n1 and n2 are smaller than root, then LCA lies in left | |
if (root->data > n1 && root->data > n2) | |
return lca(root->left, n1, n2); | |
// If both n1 and n2 are greater than root, then LCA lies in right | |
if (root->data < n1 && root->data < n2) | |
return lca(root->right, n1, n2); | |
return root; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment