-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q18_check_whether_bst_contains_dead_end.cpp
75 lines (61 loc) · 1.92 KB
/
Q18_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
// Given a Binary Search Tree that contains positive integer values greater than 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.
// Example 1:
// Input :
// 8
// / \
// 5 9
// / \
// 2 7
// /
// 1
// Output :
// Yes
// Explanation :
// Node "1" is the dead End because after that we cant insert any element.
// Example 2:
// Input :
// 8
// / \
// 7 10
// / / \
// 2 9 13
// Output :
// Yes
// Explanation :
// We can't insert any element at node 9.
#include<bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
void addAllNodes(TreeNode* root, unordered_map<int,bool>& mp) {
if(!root) return;
mp[root->val] = 1;
addAllNodes(root->left, mp);
addAllNodes(root->right, mp);
}
bool dfs(TreeNode* root, unordered_map<int,bool>& mp) {
if(!root) return false;
if(!root->left && !root->right) {
int next = root->val + 1;
int prev = root->val - 1;
if(mp.count(next) && mp.count(prev)) return true;
}
return dfs(root->left, mp) || dfs(root->right, mp);
}
bool isDeadEnd(TreeNode *root) {
unordered_map<int,bool> mp;
addAllNodes(root, mp);
mp[0] = 1;
return dfs(root, mp);
}
};
// Time Complexity : O(n)
// Space Complexity : O(n)