654. Maximum Binary Tree

题目大意:

    意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步。

    读完就知道是递归了。

    这个题真尼玛恶心,对JavaScript的友好度简直为0,用Js的可以直接放弃了。

  1. function TreeNode(val) {
  2. this.val = val
  3. this.left = this.right = null
  4. }
  5.  
  6. var constructMaximumBinaryTree = function(nums) {
  7. var ans = findmaxvalue(nums, 0, nums.length-1)
  8. return ans
  9. }
  10.  
  11. function findmaxvalue(nums, l, r)
  12. {
  13. if(r < l) {
  14. return null;
  15. }
  16. var max = nums[l];
  17. var maxpos = l;
  18. for(var i=l+1;i<=r;i++)
  19. {
  20. if(nums[i] > max) {
  21. maxpos = i
  22. max = nums[i]
  23. }
  24.  
  25. }
  26. var root = new TreeNode(max)
  27. root.left = findmaxvalue(nums, l, maxpos-1)
  28. root.right = findmaxvalue(nums, maxpos+1, r)
  29. return root;
  30. }

时间复杂度应该是小于n2的,但是我不知道这种方法的复杂度到底是多少,我猜是x(x可能是nlogn,我也不太清楚)。

如果用线段树的话,复杂度应该是x'(n < x' < x < n2

我提交了一下发现是错的,理论输出是一个数组,我tm甚至用BFS搜索了一下二叉树,加入到一个数组里了,然后我本来push为null的元素,测评机输出为一个数组???????气的一笔,随便找了份代码交了。

BFS代码:

  1. var ans = findmaxvalue(nums, 0, nums.length-1)
  2. var res = []
  3. var queue = []
  4. queue.push(ans)
  5. while(queue.length != 0) {
  6. var t = queue.shift()
  7. if(t != null) {
  8. res.push(t.val)
  9. if(t.left != null || t.right != null) {
  10. queue.push(t.left)
  11. queue.push(t.right)
  12. }
  13. }
  14. else {
  15. res.push(null)
  16. }
  17. }

654. Maximum Binary Tree的更多相关文章

  1. LeetCode - 654. Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  2. 654. Maximum Binary Tree 最大节点劈开,然后左边、右边排序

    [抄题]: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...

  3. LeetCode 654. Maximum Binary Tree最大二叉树 (C++)

    题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...

  4. 【leetcode】654. Maximum Binary Tree

    题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...

  5. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  6. 654. Maximum Binary Tree最大二叉树

    网址:https://leetcode.com/problems/maximum-binary-tree/ 参考: https://leetcode.com/problems/maximum-bina ...

  7. [LeetCode] 654. Maximum Binary Tree 最大二叉树

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  8. Python 解LeetCode:654. Maximum Binary Tree

    用一个整型数组构建一个二叉树,根结点是数组中的最大值,左右子树分别是根结点的值在数组中左右两边的部分. 分析,这是二叉树中比较容易想到的问题了,直接使用递归就行了,代码如下: class Soluti ...

  9. [LeetCode]654. Maximum Binary Tree最大堆二叉树

    每次找到数组中的最大值,然后递归的构建左右树 public TreeNode constructMaximumBinaryTree(int[] nums) { if (nums.length==0) ...

随机推荐

  1. stark组件配置,二层URL

    1.django的admin配置 2 stark组件开发 3.2层url分发 4.小结 1.django的admin配置 model.py from django.db import models # ...

  2. MySQL 通过多个示例学习索引

    最近在准备面试,关于索引这一块,发现很多以前忽略的点,这里好好整理一下 首先为什么要建立索引 一本书,有章.节.段.行这种单位. 如果现在需要找一个内容:第9章>第2节>第3段>第4 ...

  3. shell脚本--操作MySQL数据库

    其实就是一个很简单的套路,和其他语言差不多,首先连接数据库,然后在进行其他操作. 套路如下: #!/bin/bash mysql="mysql -uroot -proot" #连接 ...

  4. Leaf——美团点评分布式ID生成系统 UUID & 类snowflake

    Leaf——美团点评分布式ID生成系统 https://tech.meituan.com/MT_Leaf.html

  5. mongoDB 安装和配置环境变量,超详细版本

    下载mongoDB进行安装:https://www.mongodb.com/                                                 到Community Se ...

  6. [转帖]浏览器的F5和Ctrl+F5

    浏览器的F5和Ctrl+F5 https://www.cnblogs.com/xiangcode/p/5369084.html 在浏览器里中,按F5键和按F5同时按住Ctrl键(简称Ctrl+F5), ...

  7. SQLServer2016 之后增加了索引列数的限制 从 16个列 增加到了 32个列

    创建带有包含列的索引 https://docs.microsoft.com/zh-cn/sql/relational-databases/indexes/create-indexes-with-inc ...

  8. [转帖]Introduction to text manipulation on UNIX-based systems

    Introduction to text manipulation on UNIX-based systems https://www.ibm.com/developerworks/aix/libra ...

  9. Oracle数值函数

    --数值函数 --四舍五入 ) from dual ) from dual --数字截取 ) from dual --取模 ,) from dual

  10. zTree树形菜单使用实例

    在每个节点添加 id 和 pid, id 表示当前节点编号,pid 表示父节点编号 第一步:在页面显示菜单位置,添加 ul设置 class=”ztree” 第二步:开启简单数据格式支持 第三步:编写树 ...