-
Notifications
You must be signed in to change notification settings - Fork 0
/
week11-1.c
56 lines (51 loc) · 1.01 KB
/
week11-1.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
#include <stdio.h>
#include <stdlib.h>
struct node{
int key;
struct node * left;
struct node * right;
};
struct node * newnode(int data){
struct node * temp=(struct node *)malloc(sizeof(struct node));
temp->key=data;
temp->left=NULL;
temp->right=NULL;
return temp;
}
struct node* Insert(struct node* node, int key)
{
if (node == NULL) return newnode(key);
if (key < node->key)
node->left = Insert(node->left, key);
else if (key > node->key)
node->right = Insert(node->right, key);
return node;
}
void LCS(int a,int b,struct node * root){
struct node * curr=root;
while(1){
if(a<(curr->key)&&b<(curr->key)){
curr=curr->left;
}
else if(a>(curr->key)&&b>(curr->key)){
curr=curr->right;
}
else{
printf("%d\n",curr->key);
break;
}
}
}
int main(){
struct node * root=Insert(root,25);
Insert(root,30);
Insert(root,20);
Insert(root,10);
Insert(root,35);
Insert(root,10);
Insert(root,23);
Insert(root,5);
Insert(root,21);
Insert(root,32);
LCS(10,23,root);
}