-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathinorder-traversal-and-bst.cpp
More file actions
50 lines (44 loc) · 914 Bytes
/
inorder-traversal-and-bst.cpp
File metadata and controls
50 lines (44 loc) · 914 Bytes
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
// https://practice.geeksforgeeks.org/problems/inorder-traversal-and-bst/0
/*
Check if a list of numbers represents the inorder traversal of a BST
*/
#include <bits/stdc++.h>
using namespace std;
bool solve_test()
{
// Read size of the list
int n;
cin >> n;
// Read list
int vect[n];
// Fill array
for(int i =0; i< n; i++) cin >> vect[i];
// Case 1: Root is NULL or it has just a node
if (n <= 1)
return 1;
else if(n==2)
{
if(vect[0] < vect[1])
return 1;
return 0;
}
for(int i=0; i < n-1; i++)
{
for(int j= i+1; j < n; j++)
{
if(vect[i] == vect[j])
return 0;
else if(vect[j] < vect[i])
return 0;
}
}
return 1;
}
int main()
{
int t;
cin >> t;
while(t--)
cout<<solve_test()<<endl;
return 0;
}