This repository was archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisbst.cpp
More file actions
108 lines (96 loc) · 1.31 KB
/
isbst.cpp
File metadata and controls
108 lines (96 loc) · 1.31 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
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
103
104
105
106
107
108
#include <bits/stdc++.h>
using namespace std;
int ind;
int arr1[100];
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newnode(int val)
{
struct node* temp = new node;
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}
void preorder(struct node* root) // Preorder Traversal
{
arr1[ind] = root->data;
ind++;
if(root->left!=NULL)
{
preorder(root->left);
}
if(root->right!=NULL)
{
preorder(root->right);
}
}
struct node* insertbst(struct node* root, int val)
{
if (root == NULL)
{
struct node* temp1 = newnode(val);
return temp1;
}
if(val<root->data)
{
root->left = insertbst(root->left, val);
}
else if (val>root->data)
{
root->right = insertbst(root->right, val);
}
return root;
}
int main()
{
int t1;
scanf("%d",&t1);
int n;
int i;
for(i = 0; i<t1; i++)
{
int flag = -1;
ind = 0;
scanf("%d", &n);
int arr[n];
struct node* root = NULL;
for(int j =0; j<n;j++)
{
scanf("%d",&arr[j]);
if(j==0)
{
root = insertbst(root,arr[j]);
}
else
{
insertbst(root,arr[j]);
}
}
preorder(root);
for(int j=0;j<n;j++)
{
if(arr[j] == arr1[j])
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
if(!flag)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
return 0;
}