/
commonancestorinBST.c
46 lines (40 loc) · 1.02 KB
/
commonancestorinBST.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*****
Given the values of two nodes in a *binary search tree*,
write a c program to find the lowest common ancestor.
You may assume that both values already exist in the tree.
******/
#include <stdio.h>
typedef struct _node{
int data;
struct node* left;
struct node* right;
}node;
node* newNode(int val){
node* tmp = (node *)malloc(sizeof(node));
tmp->left = null;
tmp->right = null;
tmp->data = data;
return tmp;
}
int leastCommonAncestor(node* root, int n1, int n2){
if(root == NULL || root->data == n1 || root->data == n2)
return -1;
if((root->right != NULL && (root->right->data == n1 || root->right->data == n2)){
return root->data;
}
if((root->left != NULL && (root->left->data == n1 || root->right->data == n2)){
return root->data;
}
if(root->data > n1 && root->data < n2){
return root->data;
}
if(root->data >n1 && root->data > n2){
return leastCommonAncestor(root->left, n1, n2);
}
if(root->data <n1 && root->data < n2 ){
return leastCommonAncestor(root->right, n1, n2);
}
}
int main(){
return 0;
}