-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_if_BST_contains_DEAD_NODE.cpp
45 lines (35 loc) · 1.22 KB
/
check_if_BST_contains_DEAD_NODE.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
void inorder(Node*root, vector<int>&store, vector<int>&leaf)
{
if(root == NULL)
return;
inorder(root->left,store,leaf);
if(root -> left == NULL && root -> right == NULL)
{
// cout<<root->data<<endl;
leaf.push_back(root->data);
}
else
store.push_back(root->data);
inorder(root->right,store,leaf);
}
bool isDeadEnd(Node *root)
{
// map<Node*, Node*> parentMap;
// mapping(root,parentMap);
// return solve(root, parentMap);
vector<int>store;
vector<int> leaf;
inorder(root,store,leaf);
// now check for each leaf it contains any x + 1 and x - 1;
for(int i = 0; i<leaf.size(); i++)
{
if(leaf[i] == 1 && find(store.begin(),store.end(),2) != store.end())
return true;
/******** IMPORTANT CONCEPT ******************/
/* if a node is dead node -> then BST must contains the node-1 and node+1 value both.*/
// find the leaf[i]-1 and and leaf[i] + 1 both in the store vector
else if(find(store.begin(),store.end(),leaf[i]-1) != store.end() && find(store.begin(),store.end(),leaf[i]+1) != store.end())
return true;
}
return false;
}