-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlowestcommonancestor.c
102 lines (77 loc) · 1.94 KB
/
lowestcommonancestor.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
struct node {
int val;
struct node *left;
struct node *right;
};
typedef struct node node;
node *n1_parent = NULL;
node *n2_parent = NULL;
static int found = 0;
node* CreateBST(node *root, int val)
{
if (root == NULL) {
root = (node*)malloc(sizeof(node));
root->val = val;
root->left = NULL;
root->right = NULL;
return root;
} else if (root->val > val) {
root->left = CreateBST(root->left, val);
return root;
} else {
root->right = CreateBST(root->right, val);
return root;
}
}
void PreOrderTraversal(node *parent, node *root, int n1, int n2)
{
if (root == NULL)
return;
if (root->val == n1) {
//printf("[%d]", root->val);
n1_parent = parent;
} else if (root->val == n2) {
//printf("[%d]", root->val);
n2_parent = parent;
}
if (n1_parent != NULL && n2_parent != NULL && found ==0) {
if (n1_parent == n2_parent) {
printf("[%d]", n1_parent->val);
} else if (n1 < n2) {
printf("[%d]", n1);
} else if (n2 < n1) {
printf("[%d]", n2);
}
found = 1;
return;
}
PreOrderTraversal(root, root->left, n1, n2);
PreOrderTraversal(root, root->right, n1, n2);
}
void FindLCA(node *root, int n1, int n2)
{
node *temp_root = root;
PreOrderTraversal(root, root, n1, n2);
}
int main()
{
int num;
int val;
int k;
int n1;
int n2;
printf("Welcome to finding Lowest Common Ancestor in BST..\n");
printf("Enter number of nodes in BST...\n");
scanf("%d", &num);
node *root = NULL;
for(k=0; k < num; k++) {
scanf("%d", &val);
root = CreateBST(root, val);
}
PreOrderTraversal(root, root, n1, n2);
printf("Now time to find LCA..\n");
scanf("%d", &n1);
scanf("%d", &n2);
FindLCA(root, n1, n2);
}