-
Notifications
You must be signed in to change notification settings - Fork 0
/
isitbst.cpp
63 lines (55 loc) · 1.03 KB
/
isitbst.cpp
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
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
#define lli long long int
#define mod 1000000007
#define pb push_back
#define mk make_pair
#define fastio ios_base::sync_with_stdio(false); cout.tie(NULL);
using namespace std;
struct BST
{
int key,leftchild,rightchild;
BST()
{
key=0;
leftchild=-1;
rightchild=-1;
}
BST(int key1,int leftchild1,int rightchild1)
{
key=key1;
leftchild=leftchild1;
rightchild=rightchild1;
}
};
vector<BST> bst;
vector<int> v;
void inOrder(int root)
{
if(root==-1)
return;
inOrder(bst[root].leftchild);
v.pb(bst[root].key);
inOrder(bst[root].rightchild);
}
int main()
{
fastio;
int n,i,key, leftchild, rightchild,f=0;
cin>>n;
for (i=0;i<n;i++)
cin>>key>>leftchild>>rightchild, bst.pb(BST(key, leftchild, rightchild));
if(bst.size()>1)
{
inOrder(0);
for (i=1;i<v.size();i++)
if(v[i]<=v[i-1])
f=1;
}
if(f)
cout<<"INCORRECT";
else
cout<<"CORRECT";
return 0;
}