-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCheckDeadEnd.java
36 lines (30 loc) · 1.13 KB
/
CheckDeadEnd.java
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
/*https://practice.geeksforgeeks.org/problems/check-whether-bst-contains-dead-end/1*/
class GFG
{
static Node prev, supPrev;
static boolean result;
public static boolean isDeadEnd(Node root)
{
prev = supPrev = null;
result = false;
inOrder(root);
return result;
}
public static void inOrder(Node root)
{
if (!result && root != null)
{
inOrder(root.left);
//if prev contains 1 and root contains 2 and 1 is a leaf node, then 1 is a dead end
if (supPrev == null && prev != null && prev.data == 1 && root.data == 2 && prev.left == null && prev.right == null)
result = true;
//if prev contains x, supPreev contains x-1 and root contains x+1 and x is a leaf node then x is a dead end
if (supPrev != null && supPrev.data == prev.data-1 && prev != null && prev.data == root.data-1 && prev.left == null && prev.right == null)
result = true;
//move the pointers
supPrev = prev;
prev = root;
inOrder(root.right);
}
}
}