[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

TreeNode返回空值对应的是null

[思维问题]:

不知道dc怎么写:recursion的条件是start end就行了。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. helper函数中建了节点,很显然主函数中就不用再建了
  2. buildtree必须写root.left root.right

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

buildtree必须写root.left root.right,recursion的条件是start end就行了。

[复杂度]:Time complexity: O(nlgn) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
//corner case
if (nums == null || nums.length == 0) return null; //utilize
return constructHelper(nums, 0, nums.length - 1);
} public TreeNode constructHelper(int[] nums, int start, int end) {
//exit requirement
if (start > end) return null; //find the max
int maxIdx = start;
for (int i = start + 1; i <= end; i++) {
if (nums[i] > nums[maxIdx])
maxIdx = i;
}
TreeNode root = new TreeNode(nums[maxIdx]); //recursive in left and right
root.left = constructHelper(nums, start, maxIdx - 1);
root.right = constructHelper(nums, maxIdx + 1, end); //return
return root;
}
}

654. Maximum Binary Tree 最大节点劈开,然后左边、右边排序的更多相关文章

  1. 654. Maximum Binary Tree

    654. Maximum Binary Tree 题目大意: 意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步. 读完就知道是递归了. 这个题真尼 ...

  2. LeetCode 654. Maximum Binary Tree最大二叉树 (C++)

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

  3. 【leetcode】654. Maximum Binary Tree

    题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...

  4. LeetCode - 654. Maximum Binary Tree

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

  5. 654. Maximum Binary Tree最大二叉树

    网址:https://leetcode.com/problems/maximum-binary-tree/ 参考: https://leetcode.com/problems/maximum-bina ...

  6. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  7. [LeetCode] 654. Maximum Binary Tree 最大二叉树

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

  8. Python 解LeetCode:654. Maximum Binary Tree

    用一个整型数组构建一个二叉树,根结点是数组中的最大值,左右子树分别是根结点的值在数组中左右两边的部分. 分析,这是二叉树中比较容易想到的问题了,直接使用递归就行了,代码如下: class Soluti ...

  9. [LeetCode]654. Maximum Binary Tree最大堆二叉树

    每次找到数组中的最大值,然后递归的构建左右树 public TreeNode constructMaximumBinaryTree(int[] nums) { if (nums.length==0) ...

随机推荐

  1. 【BZOJ2555】SubString

    算是学会sam了吧…… 原题: 懒得写背景了,给你一个字符串init,要求你支持两个操作        (1):在当前字符串的后面插入一个字符串        (2):询问字符串s在当前字符串中出现了 ...

  2. uip移植telnetd并加入自己定义命令

    版权声明: https://blog.csdn.net/cp1300/article/details/30541507 刚刚移植了一下uip的telnetd,还是比較简单方便的. 首先加入文件,注意u ...

  3. js正则表达式讲的最好的

    https://www.cnblogs.com/chenmeng0818/p/6370819.html

  4. MySQL Key值(PRI, UNI, MUL)的含义

    PRI主键约束: UNI唯一约束: MUL可以重复. 参考:http://www.cnblogs.com/licheng/archive/2010/10/16/1852938.html

  5. ajax请求完成执行的操作

    var createAjax = $("#createId").ajax(function(){ //ajax操作 }); $.when(createAjax).done(func ...

  6. Oracle 语句整理

    1  查出列当中重复的值 select ip2,count(*) from vm_info group by ip2 having count(*)>1 期中ip2为列名      vm_inf ...

  7. Redis 服务端配置——Could not connect to Redis at 127.0.0.1:6379: Connection refused

    [root@centoszang 桌面]# redis-cli Could : Connection refused Could : Connection refused not connected& ...

  8. Linux运维人员最常用166个命令汇总

    引用自“菜鸟博客” 命令 功能说明 线上查询.帮助命令(2个) man 查看命令帮助,命令词典,更复杂还有info,但不常用. help 查看Linux内置命令的帮助,比如cd等命令. 文件.目录操作 ...

  9. mybatis xml中返回map 参看aiwanpai

    <!-- 指定日期活动被创建次数查询结果数据集--> <resultMap id="countPlayTimesMap" type="HashMap&q ...

  10. 到底什么是哈希Hash?

    知识点总结 ---------------------------------------------------------------------------------------------- ...