Symmetric Tree
Symmetric Tree
Given two arrays of integers: array of keys and array values associated with keys. Create a Binary Search Tree from these numbers. If the inserted key equals to the key in the current node, insert it to the right subtree.
Write a method IsSymmetric that checks whether the tree with associated values is a mirror of itself (i.e. symmetric around its center).
Write the code according to the next interface:
class TreeNode
{
public:
int key;
int val;
TreeNode *left, *right;
TreeNode(int key, int val) : key(key), val(val), left(NULL), right(NULL) {}
};
class Tree
{
public:
TreeNode *head;
Tree() : head(NULL) {};
void Insert(int key, int val); // Insert a vertex into Binary Search Tree
int IsSymmetric(void); // return 1 if Binary Search Tree from associated values is symmetric and 0 otherwise
};
// Java
class TreeNode
{
int key, val;
TreeNode left, right;
TreeNode(int key, int val)
{
this.key = key;
this.val = val;
left = right = null;
}
}
class Tree
{
TreeNode head;
Tree();
void Insert(int key, int val); // Insert a vertex into Binary Search Tree
int isSymmetric(); // return 1 if Binary Search Tree from associated values is symmetric and 0 otherwise
}
You can create (use) additional methods if needed.
Input
The first line contains number n (1 ≤ n ≤ 100). The second line contains n integers - keys: key1, key2, ..., keyn
. The third line contains n integers - the associated values val1, val2, ..., valn
. The value vali
is associated with key keyi
.
Output
Create the Binary Search Tree from input data. Print 1 if the tree with associated values is symmetric and 0 otherwise.
7 10 5 1 15 20 7 12 4 5 6 5 6 1 1
1