LeetCode 654. Maximum Binary Tree

原题链接在这里:https://leetcode.com/problems/maximum-binary-tree/description/

题目:

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

  1. The root is the maximum number in the array.
  2. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  3. 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:

Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:

      6
    /   \
   3     5
    \    / 
     2  0   
       \
        1

Note:

  1. The size of the given array will be in the range [1,1000].

题解:

找到最大max的index, 用max生成当前root, 自上而下left child recursive call 在[l, maxIndex-1]区间内找. right child recursive call 在[maxIndex+1, r]区间内找.

stop condition 是l > r时return null.

Time Complexity: O(nlogn). n = nums.length. T(n) = T(left)+T(right)+n. left+right = n, 每层traverse了nums的所有值. 如果树分的平均高度是O(logn). 

Space: O(logn). Stack space, 前提是树分的平均.

AC Java: 

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public TreeNode constructMaximumBinaryTree(int[] nums) {
12         return construct(nums, 0, nums.length-1);
13     }
14     
15     private TreeNode construct(int [] nums, int l, int r){
16         if(l > r){
17             return null;
18         }
19 
20         int maxIndex = findMaxIndex(nums, l, r);
21         TreeNode cur = new TreeNode(nums[maxIndex]);
22         cur.left = construct(nums, l, maxIndex-1);
23         cur.right = construct(nums, maxIndex+1, r);
24         return cur;
25     }
26     
27     private int findMaxIndex(int [] nums, int l, int r){
28         int res = l;
29         for(int i = l; i<=r; i++){
30             if(nums[i] > nums[res]){
31                 res = i;
32             }
33         }
34         return res;
35     }
36 }

维护一个decending order的queue. 在把用nums[i] create的当前TreeNode cur 放入queue之前,把queue尾部小于nums[i]的去掉, 并一直更新cur.left. 这是在找cur左侧的最大值当left child.

如果放入cur前queue还有值, 说明这个queue尾的值比nums[i]大,应该把cur放到queue尾node的右侧child位置上.

最后return queue首部的TreeNode, 这个是val最大的.

Time Complexity: O(nums.length). Space: O(nums.length).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public TreeNode constructMaximumBinaryTree(int[] nums) {
12         LinkedList<TreeNode> que = new LinkedList<TreeNode>();
13         for(int i = 0; i<nums.length; i++){
14             TreeNode cur= new TreeNode(nums[i]);
15             while(!que.isEmpty() && que.peekLast().val<nums[i]){
16                 cur.left = que.removeLast();
17             }
18             if(!que.isEmpty()){
19                 que.peekLast().right = cur;
20             }
21             que.addLast(cur);
22         }
23         return que.peekFirst();
24     }
25 }

 

posted @ 2017-09-21 08:48  Dylan_Java_NYC  阅读(124)  评论(0编辑  收藏  举报