Minimum Depth of Binary Tree#7464

Minimum Depth of Binary Tree

1 second
64 megabytes
Easy

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

Implement a method MinDepth that finds the minimum depth of the tree. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

prb7464.gif

Write the code according to the following interface:

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 the number val into the Binary Search Tree
    int MinDepth(void);   // Return the minimum depth of the Binary Search Tree
};

You may create and use additional methods if needed.

prb7464.gif

Input

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

Output

Create the Binary Search Tree from the input data. Find and print its minimum depth.

Examples

Example 1

Loading
Loading