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) ...
随机推荐
- asp.net mvc Areas 母版页动态获取数据进行渲染
经常需要将一些通用的页面元素抽离出来制作成母版页,但是这里的元素一般都是些基本元素,即不需要 进行后台数据交换的基本数据,但是对于一些需要通过后台查询的数据,我们应该怎么传递给前台的母版页呢 这里描述 ...
- 缓存session,cookie,sessionStorage,localStorage的区别
https://www.cnblogs.com/cencenyue/p/7604651.html(copy) 浅谈session,cookie,sessionStorage,localStorage的 ...
- C# Note30: 网络爬虫
用C#实现网络爬虫(一) 用C#实现网络爬虫(二) 基于C#.NET的高端智能化网络爬虫(一)(反爬虫哥必看) 基于C#.NET的高端智能化网络爬虫(二)(攻破携程网) C#获取网页内容的三种方式
- Flutter的Appbar
actions → List一个 Widget 列表,代表 Toolbar 中所显示的菜单,对于常用的菜单,通常使用 IconButton 来表示:对于不常用的菜单通常使用PopupMenuButto ...
- js中的arguments
了解这个对象之前先来认识一下javascript的一些功能: 其实Javascript并没有重载函数的功能,但是Arguments对象能够模拟重载.Javascrip中国每个函数都会有一个Argume ...
- RedHat 安装RabbitMQ
(以下均以root用户执行) 1.安装配置epel源rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noar ...
- java中解决小数精度问题
public class TestDouble { public static void main(String[] args) { Double d1 = 0.1; Double d2 = 0.2; ...
- ubuntu 完全卸载mysql
卸载 sudo apt-get --purge remove mysql-common -y sudo apt-get --purge remove mysql* -y sudo apt-get au ...
- endnote中文格式“,等”的修改
https://www.howsci.com/endnote-eng-cn-refer-etal.html
- socket跟TCP/IP 的关系,单台服务器上的并发TCP连接数可以有多少
常识一:文件句柄限制 在Linux下编写网络服务器程序的朋友肯定都知道每一个tcp连接都要占一个文件描述符,一旦这个文件描述符使用完了,新的连接到来返回给我们的错误是"Socket/File ...