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的更多相关文章

  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. Navicat 或者Java的JDBC通过SSH Tunnel连接MySQL数据库

    JDBC通过SSH Tunnel连接MySQL数据库 - 明明 - CSDN博客https://blog.csdn.net/a351945755/article/details/21782693 Na ...

  2. 1 Expression of Possiblity

    Expression of possibility Probably     Perhaps There's a change(that) It's very likly(that) It's pos ...

  3. linux 安装 SVN server

    安装 使用yum安装非常简单: yum install subversion 配置 2.1. 创建仓库 我们这里在/home下建立一个名为svn的仓库(repository),以后所有代码都放在这个下 ...

  4. Baby-Step-Giant-Step 很酷的算法

    Baby-Step-Giant-Step BSGS算法用于解决形如:      A  ^  x  ≡  B  (  mod  C  ) 的问题.  学这个算法前需要具备以下知识:快速幂取模.扩展欧几里 ...

  5. ActiveMQ入门案例-生产者代码实现

    <–start–> 使用Java程序操作ActiveMQ生产消息,代码的复杂度较高,但也没有默写下来的必要. 开发ActiveMQ首先需要导入activemq-all.jar包,如果是ma ...

  6. select2 简单解析

    <select name="supplierId" class="customsBrokerSel select2 absOpacity select2-hidde ...

  7. python设计模式第二十五天【访问者模式】

    1.应用场景 (1)将数据和行为进行分离,不同的角色具有不同的行为 2.代码实现

  8. Ajax的post表单,不在url后接一大串参数键值对的方法

    $('#loginForm').on('submit',function (ev) { //阻止表单参数附在url后面 ev.stopPropagation(); ev.preventDefault( ...

  9. linux 依赖解决办法

    在安装软件过程中如果出现依赖不满足,有两种情况: 1:你系统里面没有安装依赖软件,[但是你的软件源里面有这个软件,你只是没有安装], 这种情况很简单,通过 sudo apt-get install - ...

  10. Git要点

    前面的话 本文将总结Git要点 版本管理工具 [作用] 1.备份文件 2.记录历史 3.回到过去 4.对比差异 [分类] 1.手动版本控制(又叫人肉VCS) 2.LVCS 本地 3.CVCS 集中式( ...