给出一个数组,用该数组构建一个最大二叉树。
654. Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
- The root is the maximum number in the array.
- The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
- The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.
Example 1:
1 |
Input: [3,2,1,6,0,5] |
Note:
- The size of the given array will be in the range [1,1000].
算法:递归建树
每次在数组指定范围中每次选出最大元素新建节点,递归建树,其中递归建树的过程是固定写法。
1 |
public TreeNode (int[] nums) { |
近期评论