-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst3.cpp
More file actions
71 lines (69 loc) · 1.14 KB
/
bst3.cpp
File metadata and controls
71 lines (69 loc) · 1.14 KB
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
#include<iostream>
#include<stdio.h>
using namespace std;
class bst
{
public:
long long data;
bst *left,*right;
};
bst * insertInBinaryTree(bst* root,long long x)
{
if(!root)
{
bst *temp=new bst();
temp->data=x;
temp->left=temp->right=NULL;
root=temp;
return root;
}
if(root->data>x)
root->left=insertInBinaryTree(root->left,x);
else if(root->data<x)
root->right=insertInBinaryTree(root->right,x);
return root;
}
bst* LCA(bst* root,int a,int b)
{
while(1)
{
if((root->data>a&&root->data<b)||(root->data<a&&root->data>b))
return root;
if(root->data<a)
root=root->right;
else
root=root->left;
}
}
long long maxm;
void find(bst* root,int val)
{
cout<<"In FIND\n";
if(root->data>maxm)
maxm=root->data;
if(root->data==val)
return;
if(root->data<val)
find(root->right,val);
if(root->data>val)
find(root->left,val);
}
int main()
{
cout<<"IN main\n";
bst* root=NULL,*lca=NULL;
long long n,a;
scanf("%lld",&n);
for(int i=0;i<n;i++)
{
cout<<"In loop\n";
scanf("%lld",&a);
root=insertInBinaryTree(root,a);
}
scanf("%lld%lld",&a,&n);
lca=LCA(root,a,n);
find(lca,a);
find(lca,n);
printf("%lld",maxm);
return 0;
}