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) ...
随机推荐
- HDFS的命令
.....Hdfs dfs -cat path hadoop fs - 等同 1 -ls 查看当前目录的文件和文件夹 2 -lsr 递归查看 3 -du 查看文件的大小 4-dus 查看文件夹中所有的 ...
- 06_Hadoop分布式文件系统HDFS架构讲解
mr 计算框架 假如有三台机器 统领者master 01 02 03 每台机器都有过滤的应用程序 移动数据 01机== 300M >mr 移动计算 java程序传递给各个机器(mr) ...
- html总结:文本框填满表格
<style> input { width: 100%; }</style>
- PAT L3-020 至多删三个字符
https://pintia.cn/problem-sets/994805046380707840/problems/994805046946938880 给定一个全部由小写英文字母组成的字符串,允许 ...
- Git Gerrit Code Review
Gerrit Code Review | Gerrit Code Reviewhttps://www.gerritcodereview.com/
- centos安装bundle文件
centos安装VMware-Workstation-Full-*.bundle那点事 | 鳗鱼是条狗https://kinggoo.com/centos-vmware.htm Linux 下 VMW ...
- mysql问题汇总——持续更新
1.this is incompatible with sql_mode=only_full_group_by set @@sql_mode='STRICT_TRANS_TABLES,NO_ZERO_ ...
- pip ipython启动错误 Fatal error in launcher: Unable to create process using
完整的错误提示: C:\Users\yyy>ipython3Fatal error in launcher: Unable to create process using '"c:\u ...
- java类型的小知识List 等
List 复制之 浅拷贝与深拷贝 详细连接https://blog.csdn.net/never_tears/article/details/79067245 java中判断字符串是否为数字的方法的几 ...
- linux重装系统,如何保存硬盘中的内容
以前没有太关注重装系统如何保留下硬盘中的内容.但是最近有一些文件在重装系统后确实需要继续保留下来,于是花了点时间了解下磁盘分区相关的东东. 参考 http://blog.csdn.net/openn/ ...