Repository navigation
Expand file tree
/
Copy pathcheckIfArrayMatchBinarySearchTree.cpp
More file actions
125 lines (81 loc) · 2.31 KB
/
Copy pathcheckIfArrayMatchBinarySearchTree.cpp
File metadata and controls
125 lines (81 loc) · 2.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
/*
| Question: Check if a given array can represent Preorder Traversl
| of Binary Search
| Approach#1: Pass an index variable and the array, if the
| root data doesn't match the value of array[index]
| return false, otherwise increment the index by 1.
| if it's !root return true
|
|
*/
struct node{
node * left = NULL;
node * right = NULL;
node(node * l, node * r, int dat){
left = l;
right = r;
data = dat;
}
int data;
};
void printByLevel(queue <node*> myq){
if(myq.empty()) return;
queue<node*> next;
while(!myq.empty()){
node * temp = myq.front();
myq.pop();
cout<<temp->data<<" ";
if(temp->left) next.push(temp->left);
if(temp->right) next.push(temp->right);
}
cout<<endl;
printByLevel(next);
}
void printInOrder(node * root){
if (!root) return;
printInOrder(root->left);
cout<<root->data<<", ";
printInOrder(root->right);
}
void test(int &index){
index += 2;
cout<<"Inside: "<<index<<endl;
}
bool checkArrayMatchTree(node * root, int & index, vector<int> & array){
if(!root) return true;
bool left = checkArrayMatchTree(root->left,index,array);
if(root->data != array[index]){
return false;
}else{
index++;
}
bool right = checkArrayMatchTree(root->right, index, array);
return left&&right;
}
using namespace std;
int main() {
node * first = new node(NULL,NULL,1);
node * third = new node(NULL,NULL,3);
node * second= new node(first,third,2);
node * twnt = new node(NULL,NULL,20);
node * thir5 = new node(NULL,NULL, 35);
node * fourty = new node(thir5,NULL,40);
node * thirty = new node(twnt,fourty,30);
node * eighty = new node(NULL,NULL,80);
node * fifty = new node(thirty,eighty,50);
printInOrder(second);
cout<<endl;
vector<int> vec = {20,30,35,40,55,80};
vector<int> vec1 = {1,2,3};
int index = 0;
if(checkArrayMatchTree(fifty,index,vec)){
cout<<"YES!"<<endl;
}else{
cout<<"NOPE"<<endl;
}
return 0;
}