91: Maximum binary tree

Given an array of integers without duplicates. Create and return a maximum binary tree by following rules:

  • the root is the maximum number in the array
  • the left subtree is the maximum tree created from left part subarray divided by the maximum number
  • the right subtree is the maximum tree created from right part subarray divided by the maximum number

 

Example 1

Input:   [1, 3, 2] 

Output:     3
           / \
          1   2

Example 2

Input:   [3, 2, 1]
           
           

Output:     3
             \
              2
               \ 
                1
Difficulty:Medium
Topic:TreeStack
Problem #:91