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) ...
随机推荐
- 多线程系列之七:Read-Write Lock模式
一,Read-Write Lock模式 在Read-Write Lock模式中,读取操作和写入操作是分开考虑的.在执行读取操作之前,线程必须获取用于读取的锁.在执行写入操作之前,线程必须获取用于写入的 ...
- 【学习总结】Git学习-参考廖雪峰老师教程十-自定义Git
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- Hibernate two table same id
Hibernate更新数据(不用update也可以) - 森林木马 - 博客园 https://www.cnblogs.com/owenma/p/3481497.html hibernate级联更新会 ...
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例
C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...
- vue单页面模板说明文档(2)
Linter Configuration This boilerplate uses ESLint as the linter, and uses the Standard preset with s ...
- syncthing 多主机同步文件工具
周五看了下阮一峰的blog 看到有一个 syncthing的小工具挺好用的 进行了简单的尝试: 1. 下载文件位置: https://syncthing.net 2. 下载文件后的简单安装 绿色版直接 ...
- cmd远程连接oracle数据库
- Oracle数值函数
--数值函数 --四舍五入 ) from dual ) from dual --数字截取 ) from dual --取模 ,) from dual
- zTree树形菜单交互选项卡效果实现
1. 添加自定义属性 page 2. 为 ztree 每个树形节点,添加点击事件 <!DOCTYPE html> <html> <head> <meta ch ...
- /dev被异常删除的问题
今天遇到一个问题,在执行某些操作后,发现经常报“read_urandom: /dev/urandom: open failed: No such file or directory”这个错误.后来查看 ...