-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck whether BST contains Dead End.cpp
117 lines (88 loc) · 1.86 KB
/
Check whether BST contains Dead End.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
//{ Driver Code Starts
//Initial template for C++
#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);
}
// } Driver Code Ends
/*The Node structure is
struct Node {
int data;
Node * right, * left;
};*/
/*You are required to complete below method */
class Solution{
public:
bool isDeadEnd(Node *root, int maxi = INT_MAX, int mini = 1)
{
//Your code here
if(!root) return false;
if(root->data == maxi && root->data == mini) return true;
return isDeadEnd(root->left, root->data-1, mini) || isDeadEnd(root->right, maxi, root->data+1);
}
};
//{ Driver Code Starts.
// 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);
}
Solution ob;
cout<<ob.isDeadEnd(root);
cout<<endl;
}
}
// } Driver Code Ends