Given an arbitrary binary tree consisting of N nodes, each node is associated with a character value. Your task is to check whether there exist a pair of nodes such that their subtrees are equal.
Note :A subtree of a node X is all the nodes that are below X and X itself.
A binary tree is a tree consisting of at most two children.
Two subtrees are said to be equal if they both have the same structure and the corresponding nodes in both the subtrees are associated with the same character value.
The first line of each input has a single integer T, denoting the number of test cases.
The first line of each test case contains a single integer N, denoting the number of nodes in a tree.
The second line contains the keys of the nodes of the tree in the level order form ( # for NULL node) Refer to the example for further clarification.
Example:
Consider the binary tree

a
b c
d # e f
# g # # # #
# #
Explanation :
Level 1 :
The root node of the tree is a
Level 2 :
The left child of a = b
Right child of a = c
Level 3 :
Left child of b= d
Right child of b = null (#)
Left child of c = e
Right child of c = f
Level 4 :
Left child of d = null (#)
Right child of d = g
Left child of e = null (#)
Right child of e = null (#)
Left child of f = null (#)
Right child of f = null (#)
Level 5 :
Left child of g = null (#)
Right child of g = null (#)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (#).
Output format :
For each test case print on a new line “True”, if there are two similar subtrees with size greater than or equal to 2, otherwise print “False”.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 500
a <= node value <= z
Time Limit: 1 sec.
1
6
a b a c # b # # # c # # #
True
The given test case represents the following tree.

Here the subtree of 2 and 5 are the same that is
b
/
c
1
6
a a # a # a # a # a # # #
False
Considering all possible pairs
O(N ^ 3), where N is the number of nodes in the tree.
As for each pair of nodes, we are doing a preorder traversal.
O(N) , where N is the number of nodes in the tree.
As we are storing pointers to all the nodes in an array.