Problems
Same Tree
Same Tree
Given two arrays of integers. Create two Binary Search Trees from these numbers. If the inserted value equals to the current node, insert it to the right subtree.
Write a method IsSameTree that checks if two trees are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Write the code according to the next interface:
// C, C++
class TreeNode
{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Tree
{
public:
TreeNode *head;
Tree() : head(NULL) {};
void Insert(int val); // Insert number val into Binary Search Tree
int IsSameTree(Tree *p); // return 1 if current Binary Search Tree equals to p, otherwise return 0
};
// Java
class TreeNode
{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x)
{
val = x;
left = null;
right = null;
}
}
class Tree
{
TreeNode head;
Tree();
void Insert(int val); // Insert number val into Binary Search Tree
int IsSameTree(Tree p); // return 1 if current Binary Search Tree equals to p, otherwise return 0
}
You can create (use) additional methods if needed.
Input
The first line contains number n (1 ≤ n ≤ 100). The second line contains n integers.
The third line contains number m (1 ≤ m ≤ 100). The forth line contains m integers.
Output
Create two Binary Search Trees from input data. Print 1 if they are the same and 0 otherwise.
Input example #1
6 14 9 1 14 20 13 6 14 14 9 1 13 20
Output example #1
1