Maximum Depth of Binary Tree#7463

Maximum Depth of Binary Tree

1 second
128 megabytes
Easy

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

Create a method MaxDepth that finds the maximum depth of a tree. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

prb7463.gif

Write the code according to the following 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 MaxDepth(void);   // Return the maximum depth of Binary Search Tree
};
// 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 maxDepth();       // Return the maximum depth of Binary Search Tree
}

You may create (use) additional methods if needed.

Input

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

Output

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

Examples

Example 1

Loading
Loading