每次找到数组中的最大值,然后递归的构建左右树

public TreeNode constructMaximumBinaryTree(int[] nums) {
if (nums.length==0) return null;
return builder(nums,0,nums.length-1);
}
public TreeNode builder(int[] nums,int sta,int end)
{
/*
思路就是每次找到最大值,然后分为两个子数组递归构建左右树
*/
if (sta>end) return null;
int max = Integer.MIN_VALUE;
int index = -1;
for (int i = sta; i <= end ; i++) {
if (nums[i]>max)
{
max = nums[i];
index = i;
}
}
TreeNode root = new TreeNode(max);
root.left = builder(nums,sta,index-1);
root.right = builder(nums,index+1,end);
return root;
}

[LeetCode]654. Maximum Binary Tree最大堆二叉树的更多相关文章

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

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

  2. LeetCode - 654. Maximum Binary Tree

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

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

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

  4. 654. Maximum Binary Tree

    654. Maximum Binary Tree 题目大意: 意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步. 读完就知道是递归了. 这个题真尼 ...

  5. [Leetcode Week14]Maximum Binary Tree

    Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...

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

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

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

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

  8. 【leetcode】654. Maximum Binary Tree

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

  9. LeetCode 226. Invert Binary Tree (反转二叉树)

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

随机推荐

  1. 冲刺随笔——Day_Three

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 团队进行Alpha冲刺 作业正文 正文 其他参考文献 无 ...

  2. Flv.js记录(vue)

    下载 npm install flv.js 插入 import flvjs from 'flv.js'   第一种报错:Failed to read the 'buffered' property f ...

  3. Scrapy简明教程

    本文通过示例简要介绍一下使用Scrapy抓取网站内容的基本方法和流程. 继续阅读之前请确保已安装了scrapy. 基本安装方法为:pip install scrapy 我们已经在之前的文章中初步介绍了 ...

  4. Jmeter添加事务

    事务 通过第三方工具或jmeter代理录制的脚本,你会发现会录制很多的子请求.比如当打开首页后, 会继续打开图片,css,其他请求等资源文件. 通常我们会剔除掉这些子请求, 但如果我需要衡量打开一个页 ...

  5. 商品sku的排列组合

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 深入理解Java虚拟机(八)——类加载机制

    是什么是类加载机制 Java虚拟机将class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这个过程就是类加载机制. 类的生命周期 一个类从加载到内存 ...

  7. Ubuntu18开机执行shell命令

    1.打开shell终端,输入 sudo vi /etc/rc.local 2.在编辑器里面输入自己要启动的脚本,特别强调:脚本(程序)要有可执行权限 #!/bin/bash echo "ru ...

  8. SQL注入 (一)

    一.手工注入 注入点检测 识别方法:通过在url栏输入'.and 1=1.or 1=1.and sleep(1),如果出现报错,或者页面不一致,则可能存在注入点. 注入类型:整数型.字符型.搜索型.B ...

  9. .NET5下的三维应用程序开发

    终于等到了.NET5的发布,怀着激动的心情体验了一下:"香"就一个字. 如何基于.NET5开发工业软件,也广大三维应用开发者关心的问题.我们的Rapid SDK已经率先支持.NET ...

  10. Redis数据持久化(RDB、AOF)

    1. 简介   Redis作为内存型数据库,数据都保存在内存中,如果重启或意外宕机后,数据会全部丢失.因此,Redis提供了完善的持久化机制,将内存中的数据持久化到磁盘上,避免了完整性和安全性的问题, ...