Symmetric Tree#7467

Symmetric Tree

1 second
128 megabytes
Very easy

Given two arrays of integers: array of keys and array of 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).

prb7467.gif

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
};
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 (1 \le n \le 100). The second line contains integers - keys: key[1], key[2], ..., key[n]. The third line contains 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 if the tree with associated values is symmetric and otherwise.

Examples

Example 1

Loading
Loading