using
System;
using
System.Collections.Generic;
class
GFG
{
public
class
Node
{
public
char
key;
public
Node left, right;
};
static
Node newNode(
char
key)
{
Node node =
new
Node();
node.key = key;
node.left = node.right =
null
;
return
node;
}
static
HashSet<String> subtrees =
new
HashSet<String>();
static
bool
dupSubUtil(Node root)
{
Queue<Node> bfs =
new
Queue<Node>();
bfs.Enqueue(root);
while
(bfs.Count != 0)
{
Node n = bfs.Peek();
bfs.Dequeue();
char
l =
' '
, r =
' '
;
if
(n.left !=
null
)
{
l = n.left.key;
bfs.Enqueue(n.left);
}
if
(n.right !=
null
)
{
r = n.right.key;
bfs.Enqueue(n.right);
}
String subt =
""
;
subt += n.key;
subt += l;
subt += r;
if
(l !=
' '
|| r !=
' '
)
{
if
(!subtrees.Contains(subt))
{
return
true
;
}
}
}
return
false
;
}
public
static
void
Main(String[] args)
{
Node root = newNode(
'A'
);
root.left = newNode(
'B'
);
root.right = newNode(
'C'
);
root.left.left = newNode(
'D'
);
root.left.right = newNode(
'E'
);
root.right.right = newNode(
'B'
);
root.right.right.right = newNode(
'E'
);
root.right.right.left = newNode(
'D'
);
if
(dupSubUtil(root))
Console.WriteLine(
"Yes"
);
else
Console.WriteLine(
"No"
);
}
}