eolymp
bolt
Try our new interface for solving problems
Problems

Minimum Depth of Binary Tree

Minimum Depth of Binary Tree

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 a method MinDepth that finds the minimum depth of a 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 next 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 number val to Binary Search Tree

int MinDepth(void); // Return the minimum depth of a Binary Search Tree

};

You can create (use) additional methods if needed.

prb7464.gif

Input

The first line contains number n (1n100). The second line contains n integers.

Output

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

Time limit 1 second
Memory limit 64 MiB
Input example #1
4
14 14 9 1
Output example #1
2
Author Mykhailo Medvediev
Source C++ Language