Skip to content

Commit b6c3f8f

Browse files
committed
Check whether BST contains Dead End
1 parent 42f0336 commit b6c3f8f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

DSA Crack Sheet/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
- [Replace every element with the least greater element on its right](https://www.geeksforgeeks.org/replace-every-element-with-the-least-greater-element-on-its-right/ "view question")
221221
- []( "view question") - [Cpp Solution](./solutions/.cpp)
222222
- [Preorder to BST](https://practice.geeksforgeeks.org/problems/preorder-to-postorder4423/1# "view question") - [Cpp Solution](./solutions/Preorder%20to%20BST.cpp)
223+
- [Check whether BST contains Dead End](https://practice.geeksforgeeks.org/problems/check-whether-bst-contains-dead-end/1 "view question") - [Cpp Solution](./solutions/Check%20whether%20BST%20contains%20Dead%20End.cpp)
223224
- []( "view question") - [Cpp Solution](./solutions/.cpp)
224225

225226
### Greedy Method
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Check whether BST contains Dead End
3+
===================================
4+
5+
Given a Binary search Tree that contains positive integer values greater then 0. The task is to complete the function isDeadEnd which returns true if the BST contains a dead end else returns false. Here Dead End means, we are not able to insert any element after that node.
6+
7+
Examples:
8+
Input :
9+
8
10+
/ \
11+
5 9
12+
/ \
13+
2 7
14+
/
15+
1
16+
17+
Output : Yes
18+
Explanation : Node "1" is the dead End because after that
19+
we cant insert any element.
20+
21+
Input :
22+
8
23+
/ \
24+
7 10
25+
/ / \
26+
2 9 13
27+
28+
Output : Yes
29+
Explanation : We can't insert any element at
30+
node 9.
31+
32+
Input:
33+
The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting the no of nodes of the BST . Second line of each test case consists of 'N' space separated integers denoting the elements of the BST. These elements are inserted into BST in the given order.
34+
35+
Output:
36+
The output for each test case will be 1 if the BST contains a dead end else 0.
37+
38+
Constraints:
39+
1<=T<=100
40+
1<=N<=200
41+
42+
Example(To be used only for expected output):
43+
Input:
44+
2
45+
6
46+
8 5 9 7 2 1
47+
6
48+
8 7 10 9 13 2
49+
Output:
50+
1
51+
1
52+
*/
53+
54+
void inorder(Node *root, unordered_set<int> &vis, unordered_set<int> &leaf)
55+
{
56+
if (!root)
57+
return;
58+
vis.insert(root->data);
59+
if (!root->left && !root->right)
60+
leaf.insert(root->data);
61+
inorder(root->left, vis, leaf);
62+
inorder(root->right, vis, leaf);
63+
}
64+
65+
bool isDeadEnd(Node *root)
66+
{
67+
unordered_set<int> vis, leaf;
68+
vis.insert(0);
69+
inorder(root, vis, leaf);
70+
71+
for (auto &i : leaf)
72+
{
73+
if (vis.count(i - 1) && vis.count(i + 1))
74+
return true;
75+
}
76+
return false;
77+
}

0 commit comments

Comments
 (0)