654. Maximum Binary Tree
654. Maximum Binary Tree

题目大意:
意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步。
读完就知道是递归了。
这个题真尼玛恶心,对JavaScript的友好度简直为0,用Js的可以直接放弃了。
function TreeNode(val) {
this.val = val
this.left = this.right = null
}
var constructMaximumBinaryTree = function(nums) {
var ans = findmaxvalue(nums, 0, nums.length-1)
return ans
}
function findmaxvalue(nums, l, r)
{
if(r < l) {
return null;
}
var max = nums[l];
var maxpos = l;
for(var i=l+1;i<=r;i++)
{
if(nums[i] > max) {
maxpos = i
max = nums[i]
}
}
var root = new TreeNode(max)
root.left = findmaxvalue(nums, l, maxpos-1)
root.right = findmaxvalue(nums, maxpos+1, r)
return root;
}
时间复杂度应该是小于n2的,但是我不知道这种方法的复杂度到底是多少,我猜是x(x可能是nlogn,我也不太清楚)。
如果用线段树的话,复杂度应该是x'(n < x' < x < n2)
我提交了一下发现是错的,理论输出是一个数组,我tm甚至用BFS搜索了一下二叉树,加入到一个数组里了,然后我本来push为null的元素,测评机输出为一个数组???????气的一笔,随便找了份代码交了。
BFS代码:
var ans = findmaxvalue(nums, 0, nums.length-1)
var res = []
var queue = []
queue.push(ans)
while(queue.length != 0) {
var t = queue.shift()
if(t != null) {
res.push(t.val)
if(t.left != null || t.right != null) {
queue.push(t.left)
queue.push(t.right)
}
}
else {
res.push(null)
}
}
654. Maximum Binary Tree的更多相关文章
- 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 最大节点劈开,然后左边、右边排序
[抄题]: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
- 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 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 654. Maximum Binary Tree最大二叉树
网址:https://leetcode.com/problems/maximum-binary-tree/ 参考: https://leetcode.com/problems/maximum-bina ...
- [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) ...
随机推荐
- scroll滚动条样式修改
一般我们有两种情况会出现滚动条,一种是overflow,一种是使用scroll. 当我们需要改变这个滚动条样式的时候,我们需要做以下的修改: html: <div id="style- ...
- SAP配置BOM的适用范围
配置BOM中定义属性,单纯的编码要搞死人: 适合小批量周期短多品种
- 使用python库xlsxwriter库来输出各种xlsx文件
功能性的文章直接用几个最简单的实现表达: xlsxwriter库的核心就是其Workbook对象. 创建一个指定名字的xlsx文件: import xlsxwriter filename = '/Us ...
- 利用Python制作简单的小程序:IP查看器
前言 说实话,查看电脑的IP,也挺无聊的,但是够简单,所以就从这里开始吧.IP地址在操作系统里就可以直接查看.但是除了IP地址,我们也想通过IP获取地理地址和网络运营商情况.IP地址和地理地址并没有固 ...
- windows 10 screenshot keyboard shortcut
windows 10 screenshot keyboard shortcut Win + Shfit + S https://www.cnet.com/how-to/8-ways-to-take-s ...
- WampServer的安装和配置
1.安装WampServer 启动时发现WampServer的图标是红色的,状态为put offline状态:发现无法put online,并报错could not found the menu it ...
- curl 命令 从文件读取参数
-d @filename 从文件读入内容-d @- 从stdin读入内容 -x localhost:8888 加上fiddler代理 一个sample curl -K api.conf -d @b ...
- volatile和synchronized的区别
volatile和synchronized特点 首先需要理解线程安全的两个方面:执行控制和内存可见. 执行控制的目的是控制代码执行(顺序)及是否可以并发执行. 内存可见控制的是线程执行结果在内存中对其 ...
- 数据库迁移(创建关联等操作) Target database is not up to date报错
使用Mysql-sqlalchemy执行数据库迁移 来更新数据库: 队长试探性的在网上找了几种方案 依然没有解决报错问题: 后来看了https://www.aliyun.com/jiaocheng/4 ...
- SpringBoot之显示本地图片范例
controller // 扫描指定目录下的图片进行展示 @RequestMapping("/showPics") public ModelAndView showPics(Mod ...