-
Notifications
You must be signed in to change notification settings - Fork 1
/
232.cpp
131 lines (96 loc) · 2.22 KB
/
232.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
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
126
127
128
129
130
131
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left;
struct Node *right;
Node(int x){
data = x;
left = NULL;
right = NULL;
}
};
void insert(Node ** tree, int val)
{
Node *temp = NULL;
if(!(*tree))
{
temp = new Node(val);
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
int getCountOfNode(Node *root, int l, int h)
{
if (!root) return 0;
if (root->data == h && root->data == l)
return 1;
if (root->data <= h && root->data >= l)
return 1 + getCountOfNode(root->left, l, h) +
getCountOfNode(root->right, l, h);
else if (root->data < l)
return getCountOfNode(root->right, l, h);
else return getCountOfNode(root->left, l, h);
}
bool isDeadEnd(Node *root);
int main()
{
int T;
cin>>T;
while(T--)
{
Node *root;
Node *tmp;
//int i;
root = NULL;
int N;
cin>>N;
for(int i=0;i<N;i++)
{
int k;
cin>>k;
insert(&root, k);
}
cout<<isDeadEnd(root);
cout<<endl;
}
}
// } Driver Code Ends
/*The Node structure is
struct Node {
int data;
Node * right, * left;
};*/
/*You are required to complete below method */
void traverse(Node *root,unordered_set<int> &leafs,unordered_set<int> &nodes){
if(!root) return;
nodes.insert(root->data);
if(!root->left && !root->right){
leafs.insert(root->data);
return;
}
traverse(root->left,leafs,nodes);
traverse(root->right,leafs,nodes);
}
bool isDeadEnd(Node *root)
{
if (root == NULL) return false;
unordered_set<int> leafs,nodes;
nodes.insert(0);
traverse(root,leafs,nodes);
for(auto i=leafs.begin();i!=leafs.end();i++){
int x=(*i);
if(nodes.find(x-1)!=nodes.end() && nodes.find(x+1)!=nodes.end()) return true;
}
return false;
}