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) ...
随机推荐
- stark组件配置,二层URL
1.django的admin配置 2 stark组件开发 3.2层url分发 4.小结 1.django的admin配置 model.py from django.db import models # ...
- MySQL 通过多个示例学习索引
最近在准备面试,关于索引这一块,发现很多以前忽略的点,这里好好整理一下 首先为什么要建立索引 一本书,有章.节.段.行这种单位. 如果现在需要找一个内容:第9章>第2节>第3段>第4 ...
- shell脚本--操作MySQL数据库
其实就是一个很简单的套路,和其他语言差不多,首先连接数据库,然后在进行其他操作. 套路如下: #!/bin/bash mysql="mysql -uroot -proot" #连接 ...
- Leaf——美团点评分布式ID生成系统 UUID & 类snowflake
Leaf——美团点评分布式ID生成系统 https://tech.meituan.com/MT_Leaf.html
- mongoDB 安装和配置环境变量,超详细版本
下载mongoDB进行安装:https://www.mongodb.com/ 到Community Se ...
- [转帖]浏览器的F5和Ctrl+F5
浏览器的F5和Ctrl+F5 https://www.cnblogs.com/xiangcode/p/5369084.html 在浏览器里中,按F5键和按F5同时按住Ctrl键(简称Ctrl+F5), ...
- SQLServer2016 之后增加了索引列数的限制 从 16个列 增加到了 32个列
创建带有包含列的索引 https://docs.microsoft.com/zh-cn/sql/relational-databases/indexes/create-indexes-with-inc ...
- [转帖]Introduction to text manipulation on UNIX-based systems
Introduction to text manipulation on UNIX-based systems https://www.ibm.com/developerworks/aix/libra ...
- Oracle数值函数
--数值函数 --四舍五入 ) from dual ) from dual --数字截取 ) from dual --取模 ,) from dual
- zTree树形菜单使用实例
在每个节点添加 id 和 pid, id 表示当前节点编号,pid 表示父节点编号 第一步:在页面显示菜单位置,添加 ul设置 class=”ztree” 第二步:开启简单数据格式支持 第三步:编写树 ...