-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion52.c
68 lines (57 loc) · 1.22 KB
/
question52.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
LCA for binary tree
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *right;
struct node *left;
};
struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int LCA(struct node *root, int n1, int n2){
if(!root){
return -1;
}
int data = root->data;
if(data == n1){
return n1;
}
if(data == n2){
return n2;
}
int left = LCA(root->left, n1, n2);
int right = LCA(root->right, n1, n2);
if(left == n1 && right == n2){
return root->data;
}
return (left > 0)?left: right;
}
int main(){
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->right->left = newNode(7);
root->right->right = newNode(8);
root->right->right->left = newNode(13);
root->right->right->right = newNode(14);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->left->left->left = newNode(9);
root->left->left->right = newNode(10);
root->left->right->left = newNode(11);
root->left->right->right = newNode(12);
int res = LCA(root,8,13);
if(res > 0){
printf("LCA is....%d \n", res);
}else{
printf("not present\n");
}
return 0;
}