Minimum and Maximum in a tree#4034

Minimum and Maximum in a tree

1 second
122.174 megabytes
Easy

Given an array of integers. Create a Binary Search Tree from these numbers. If the inserted value equals to the current node, insert it to the right subtree.

Write methods Min and Max that find a minimum and a maximum element in a tree.

prb7463.gif

Write the code according to the next interface:

// C++
class TreeNode
{
public:
    int val;
    TreeNode *left, *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 to Binary Search Tree
    int Min(void); // Return minimum in a tree
    int Max(void); // Return maximum in a tree
};

You can create (use) additional methods if needed.

Input

The first line contains number (1 \leq n \leq 100). The second line contains integers.

Output

Create the Binary Search Tree from input data. Print in one line the Minimum and the Maximum element in a tree.

Examples

Example 1

Loading
Loading