-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst2.cpp
More file actions
73 lines (69 loc) · 1.16 KB
/
bst2.cpp
File metadata and controls
73 lines (69 loc) · 1.16 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
72
#include<iostream>
using namespace std;
class bst
{
public:
long long data;
bst *left,*right;
};
bst * insertInBinaryTree(bst* root,int x,int flag)
{
cout<<"Inserting"<<x<<endl;
if(!root&&!flag)
{
bst *temp=new bst();
temp->data=x;
temp->left=temp->right=NULL;
root=temp;
return root;
}
if(!root&&flag)
{
bst *temp=new bst();
temp->data=x;
temp->left=temp->right=NULL;
root=temp;
cout<<"NO\n";
return root;
}
if(root->data>x)
root->left=insertInBinaryTree(root->left,x,flag);
else if(root->data<x)
root->right=insertInBinaryTree(root->right,x,flag);
else
{
if(flag)
cout<<"YES\n";
}
return root;
}
void inorder(bst* root)
{
if(root->left)
inorder(root->left);
cout<<root->data<<" ";
if(root->right)
inorder(root->right);
}
int main()
{
long long t,n,m,a;
cin>>t;
while(t--)
{
bst* root;
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>a;
root=insertInBinaryTree(root,a,0);
}
inorder(root);
for(int i=0;i<m;i++)
{
cin>>a;
root=insertInBinaryTree(root,a,1);
}
}
return 0;
}