[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 ...
随机推荐
- C语言const和define的区别
const 定义的是变量不是常量,只是这个变量的值不允许改变是常变量!带有类型.编译运行的时候起作用存在类型检查. define 定义的是不带类型的常数,只进行简单的字符替换.在预编译的时候起作用,不 ...
- django项目初始化
1.为了方便管理app,我们添加专门的apps文件夹来存放所有的app.结构如下 1.1设置完apps文件夹以后我们需要对配置文件做相应的更改 1.1.1.在seetings.py里添加django文 ...
- 为什么Python中称__lt__、__gt__等为“富比较”方法
Python中基类object提供了一系列可以用于实现同类对象进行"比较"的方法,可以用于同类对象的不同实例进行比较,包括__lt__.__gt__.__le__.__ge__._ ...
- PyQt(Python+Qt)学习随笔:formLayout的layoutRowWrapPolicy属性
Qt Designer的表单布局(formLayout)中,layoutRowWrapPolicy用于控制表单布局中表单行的标签和输入部件之间是否换行.如图: 上图中蓝色标记圈起来的下拉列表数据是其可 ...
- Oracle表操作-创建及增删改查
数据类型: 1.CHAR:定长字符类型,默认长度是1,最长不超过2000字节. 2.CARCHAR2(length):可变字符类型,默认长度是1,最长不超过4000字符. 3.NUMBER(P,S): ...
- Leetcode学习笔记(4)
题目1 ID121 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股 ...
- Leetcode学习笔记(3)
题目1 ID88 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量 ...
- element ui中表单循环项的校验
注意:prop是动态的
- 2020中国.NET开发者峰会主题内容发布
2020年12月09日,组委会正式发布了China .NET Conf 2020中国 .NET 开发者峰会的主题内容. 今年的大会主题收到超预期的主题,无论是数量还是质量上都比2019年有所进步,这也 ...
- AtCoder Regular Contest 108
Contest Link Official Editorial A - Sum and Product Given are integers \(S\) and \(P\) . Is there a ...