[Leetcode Week14]Maximum Binary Tree
Maximum Binary Tree 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/maximum-binary-tree/description/
Description
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
- The root is the maximum number in the array.
- The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
- The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.
Example
Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:
6
/ \
3 5
\ /
2 0
\
1
Note: The size of the given array will be in the range [1,1000].
Solution
class Solution {
public:
TreeNode* getSubTree(vector<int>& nums, int start, int end) {
TreeNode* resultNode;
if (start == end) {
resultNode = new TreeNode(nums[start]);
return resultNode;
}
int maxIdx = start;
int i;
for (i = start; i <= end; i++) {
if (nums[i] > nums[maxIdx])
maxIdx = i;
}
resultNode = new TreeNode(nums[maxIdx]);
if (maxIdx > start) {
resultNode -> left = getSubTree(nums, start, maxIdx - 1);
}
if (maxIdx < end) {
resultNode -> right = getSubTree(nums, maxIdx + 1, end);
}
return resultNode;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
if (nums.empty())
return NULL;
return getSubTree(nums, 0, nums.size() - 1);
}
};
解题描述
这道题的题意是,对给定的一个数组,构造一棵所谓的“最大二叉树”。很容易想到的就是使用递归的思想,每次都对数组的一段进行处理,找出数组段中最大的元素,将该元素所谓当前树的树根,对元素左右两边两个数组段分别构造“最大二叉树”,分别作为树根的左子树和右子树。
[Leetcode Week14]Maximum Binary Tree的更多相关文章
- LeetCode - 654. Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 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题解Maximum Binary Tree
1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...
- [LeetCode]654. Maximum Binary Tree最大堆二叉树
每次找到数组中的最大值,然后递归的构建左右树 public TreeNode constructMaximumBinaryTree(int[] nums) { if (nums.length==0) ...
- Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)
Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- leetcode_998. Maximum Binary Tree II
https://leetcode.com/problems/maximum-binary-tree-ii/ 在654. Maximum Binary Tree版本的建树基础上,在最后插入一个数. 新节 ...
随机推荐
- html/css/javascript知识点集锦;完全小白开搞web编程
知识点集锦 1.在html模板的代码中会有下面一类: {% block title %} Blog entries {% endblock %}这里的 block 和 endblock 是神马? {% ...
- 如何实时获取DBGrid 中当前单元格输入的内容?
如何获取DBGrid 中当前单元格输入的内容? 还没输入完成,我想实时获取 Cell中的内容,以便作其他处理, 用什么事件呢? 所以Field的Onchange事件是没用的. 这个问题简单啊,每输入1 ...
- Java SE1.6中的Synchronized
1 引言 在多线程并发编程中Synchronized一直是元老级角色,很多人都会称呼它为重量级锁,但是随着Java SE1.6对Synchronized进行了各种优化之后,有些情况下它并不那么重了,本 ...
- 第50天:scrollTo小火箭返回顶部
scrollTo(x,y)//可把内容滚动到指定的坐标scrollTo(xpos,ypos)//x,y值必需 1.固定导航栏 <!DOCTYPE html> <html lang=& ...
- FullCalendar(日程管理控件)
(以下是我学习FullCalendar控件时,网络上收集的一些资料) jQuery.fullCalendar官方网址: http://arshaw.com/fullcalendar/ http://a ...
- BZOJ4813 CQOI2017小Q的棋盘(树形dp)
设f[i][j]为由i号点开始在子树内走j步最多能经过多少格点,g[i][j]为由i号点开始在子树内走j步且回到i最多能经过多少格点,转移显然. #include<iostream> #i ...
- Luogu2540 斗地主增强版(搜索+动态规划)
单纯的暴搜似乎还是很好写的,然而过不了.出完顺子之后答案是可以dp出来的,于是大力搜然后大力dp就好了. dp时强行讨论完了几乎所有拆牌情况,理性愉悦一发. #include<iostream& ...
- BZOJ4565 HAOI2016字符合并(区间dp+状压dp)
设f[i][j][k]为将i~j的字符最终合并成k的答案.转移时只考虑最后一个字符是由哪段后缀合成的.如果最后合成为一个字符特殊转移一下. 复杂度看起来是O(n32k),实际常数极小达到O(玄学). ...
- [洛谷P1452]Beauty Contest
题目大意:给你$n$个点,求出其中最远点的距离 题解:求出凸包,最远点一定都在凸包上,可以对每条边求出最远的点(可以双指针),然后求出和这条边的端点的距离,更新答案 卡点:最开始对每个点求出最远点,但 ...
- BZOJ5285 & 洛谷4424 & UOJ384:[HNOI/AHOI2018]寻宝游戏——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5285 https://www.luogu.org/problemnew/show/P4424 ht ...