import
java.io.*;
import
java.util.*;
class
Node {
int
data;
Node left, right;
Node(
int
data)
{
this
.data = data;
left = right =
null
;
}
}
class
GFG {
Node root;
void
BST() { root =
null
; }
Node insert(Node node,
int
key)
{
if
(node ==
null
) {
return
new
Node(key);
}
if
(key < node.data) {
node.left = insert(node.left, key);
}
else
if
(key > node.data) {
node.right = insert(node.right, key);
}
return
node;
}
void
findAllNodes(Node root,
Map<Integer, Integer> allNodes)
{
if
(root ==
null
) {
return
;
}
allNodes.put(root.data,
1
);
findAllNodes(root.left, allNodes);
findAllNodes(root.right, allNodes);
}
boolean
check(Node root, Map<Integer, Integer> allNodes)
{
if
(root ==
null
) {
return
false
;
}
if
(root.left ==
null
&& root.right ==
null
) {
int
pre = root.data -
1
;
int
next = root.data +
1
;
if
(allNodes.containsKey(pre)
&& allNodes.containsKey(next)) {
return
true
;
}
}
return
check(root.left, allNodes)
|| check(root.right, allNodes);
}
boolean
isDeadEnd(Node root)
{
if
(root ==
null
) {
return
false
;
}
Map<Integer, Integer> allNodes
=
new
HashMap<Integer, Integer>();
allNodes.put(
0
,
1
);
findAllNodes(root, allNodes);
return
check(root, allNodes);
}
public
static
void
main(String[] args)
{
GFG tree =
new
GFG();
Node root =
null
;
root = tree.insert(root,
8
);
root = tree.insert(root,
5
);
root = tree.insert(root,
2
);
root = tree.insert(root,
3
);
root = tree.insert(root,
7
);
root = tree.insert(root,
11
);
root = tree.insert(root,
4
);
if
(tree.isDeadEnd(root) ==
true
) {
System.out.println(
"Yes"
);
}
else
{
System.out.println(
"No"
);
}
}
}