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:
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就行了。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- helper函数中建了节点,很显然主函数中就不用再建了
- 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 最大节点劈开,然后左边、右边排序的更多相关文章
- 654. Maximum Binary Tree
654. Maximum Binary Tree 题目大意: 意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步. 读完就知道是递归了. 这个题真尼 ...
- LeetCode 654. Maximum Binary Tree最大二叉树 (C++)
题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...
- 【leetcode】654. Maximum Binary Tree
题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
- LeetCode - 654. Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- 654. Maximum Binary Tree最大二叉树
网址:https://leetcode.com/problems/maximum-binary-tree/ 参考: https://leetcode.com/problems/maximum-bina ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- [LeetCode] 654. Maximum Binary Tree 最大二叉树
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- Python 解LeetCode:654. Maximum Binary Tree
用一个整型数组构建一个二叉树,根结点是数组中的最大值,左右子树分别是根结点的值在数组中左右两边的部分. 分析,这是二叉树中比较容易想到的问题了,直接使用递归就行了,代码如下: class Soluti ...
- [LeetCode]654. Maximum Binary Tree最大堆二叉树
每次找到数组中的最大值,然后递归的构建左右树 public TreeNode constructMaximumBinaryTree(int[] nums) { if (nums.length==0) ...
随机推荐
- [Java] Thread -- 避免Race Condition (Synchronized)
public class TestRaceCondition implements Runnable{ public static int counter = 0; public static voi ...
- python 保存对象文件
之前用 R 语言一直感觉 .Rdata 格式的文件很好用,可以把每次执行的中间文件保存便于下次调用,刚熟悉 Python 还没接触这块知识,所以有时候做项目不太顺手,索性上网搜了下,整理如下: 模型存 ...
- day 12
一,什么是装饰器? 装饰器本质上就是一个python函数,他可以让其他函数在不需要做任何代码变动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用场景:比如插入日志,性能测试,事 ...
- 对象属性的描述:writable、enumerable、configurable
writable属性 writable属性是一个布尔值,决定了目标属性的值(value)是否可以被改变.如果原型对象的某个属性的writable为false,那么子对象将无法自定义这个属性. enum ...
- debian系统下apache2开启ssi功能
SSI (Server Side Include)的 html 文件扩展名 (.shtml), 通常称为"服务器端嵌入"或者叫"服务器端包含"说白了就是类似其他 ...
- 高性能网络编程之IO和NIO阻塞分析
一.内容 1.阻塞和非阻塞是什么? 2.传统IO模型,他存在哪些阻塞点 3.NIO模型 4.对比总结 1.阻塞和非阻塞是什么? 阻塞:做某件事情,直到完成,除非超时,如果没有完成,继续等待. 非阻塞: ...
- SqlDbx 个人版本使用指定的instant client
set Oracle_Home=D:\Toolkit\Oracle kits\instantclient_x86 set TNS_ADMIN=%Oracle_Home% set NLS_LANG=SI ...
- Hadoop 权限管理
Hadoop的权限管理同Linux的很像,有用户,用户组之分,同时Hadoop提供了权限管理命令,主要包括: chmod [-R] mode file … 只有文件的所有者或者超级用户才有权限改变文件 ...
- Lepus监控之安装部署
PHP和Python都是跨平台的语言,所以理论上系统应该可以支持在不同的平台上运行.但是由于时间和精力以及资源有限,目前天兔系统只测试完善了Centos/RedHat系统的支持.我们目前提供的技术支持 ...
- 第24课 可变参数模板(5)_DllHelper和lambda链式调用
1. dll帮助类 (1)dll的动态链接 ①传统的调用方式:先调用LoadLibrary来加载dll,再定义函数指针类型,接着调用GetProcAddress获取函数地址.然后通过函数指针调用函数, ...