-
Notifications
You must be signed in to change notification settings - Fork 1
/
Check_whether_BST_contains_Dead_End.cpp
65 lines (53 loc) · 1.84 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
/*
Problem Statement:
-----------------
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.
Examples:
---------
Input :
8
/ \
5 9
/ \
2 7
/
1
Output : Yes
Explanation : Node "1" is the dead End because after that we cant insert any element.
Input :
8
/ \
7 10
/ / \
2 9 13
Output : Yes
Explanation : We can't insert any element at node 9.
Input: 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.
Output: The output for each test case will be 1 if the BST contains a dead end else 0.
*/
// Link --> https://practice.geeksforgeeks.org/problems/check-whether-bst-contains-dead-end/1#
// Code:
void isDeadUtil(Node* root , int l , int h , bool &answer)
{
if(root == NULL)
return;
if(answer)
return;
if(root->left == NULL and root->right == NULL)
{
if(l == h)
answer = true;
return;
}
isDeadUtil(root->left , l , root->data-1 , answer);
isDeadUtil(root->right , root->data+1 , h , answer);
}
bool isDeadEnd(Node *root)
{
bool answer = false;
isDeadUtil(root , 1 , INT_MAX , answer);
return answer;
}