[LeetCode]654. Maximum Binary Tree最大堆二叉树
每次找到数组中的最大值,然后递归的构建左右树
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最大堆二叉树的更多相关文章
- 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 follo ...
- [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
654. Maximum Binary Tree 题目大意: 意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步. 读完就知道是递归了. 这个题真尼 ...
- [Leetcode Week14]Maximum Binary Tree
Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...
- 【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 ...
- 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 ...
随机推荐
- javascript开发后端程序的神器nodejs
目录 简介 nodejs的历史 nodejs简介 nodejs的运行环境 process 终止进程 env argv CLI交互 exports模块 nodejs API nodejs的框架 简介 j ...
- msql语句
表相关语句: 创表相关 CREATE TABLE `Student`( `s_id` VARCHAR(20), `s_name` VARCHAR(20) NOT NULL DEFAULT '', `s ...
- java ipv6发邮件需要注意的点
和ipv4发邮件一样,毕竟ip只是用来找地址的,v4 v6使用上基本没区别. 但有一点得注意:java ipv6采用发送RST包来通知邮件服务器断开连接,这样会导致客户端抛 MessagingExce ...
- moviepy音视频剪辑:AudioClip帧处理时报TypeError: only size-1 arrays can be converted to Python scalar错
☞ ░ 前往老猿Python博文目录 ░ 一.环境 操作系统:win7 64位 moviepy:1.0.3 numpy:1.19.0 Python:3.7.2 二.应用代码及报错信息 程序代码 if ...
- PyQt(Python+Qt)学习随笔:QTableWidget设置项的setItem方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTableWidget表格部件中setItem用于在表格部件QTableWidget创建后,设定指 ...
- requests的再次学习
title: requests模块的再次理解 date: 2020-03-10 22:44:26 tags: 1.response的解析 当requests模块发送请求后,我们会对其响应的数据也就是r ...
- 熊海CMS xhcms v1.0代码审计
有空的时候就进行小型CMS的代码审计,这次审计的对象是熊海CMS v1.0 本地环境安装好了之后,可以看到提示安装了锁文件 说明重装漏洞应该不会存在了,这时候丢进seay代码审计系统的代码也出结果了, ...
- kickstart 谷歌 D 2020 年 7 月 12 日 13: 00 - 16: 00
https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff08/0000000000386d5c (kick st ...
- filebeat输出结果到elasticsearch的多个索引
基本环境: filebeat版本:6.5.4 (Linux,x86-64) elasticsearch版本:6.54 (一)需求说明 在一台服务器上有多个日志需要使用filebeat日志收集到el ...
- 新挖个坑,准备学习一下databricks的spark博客
挖坑 https://databricks.com/blog 一.spark3.0特性(Introducing Apache Spark 3.0) 1.通过通过自适应查询执行,动态分区修剪和其他优化使 ...