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: key[1], key[2], ..., key[n]
. The third line contains n integers - the associated values val[1], val[2], ..., val[n]
. The value val[i]
is associated with key key[i]
.
Output
Create the Binary Search Tree from input data. Print 1 if the tree with associated values is symmetric and 0 otherwise.