[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版本的建树基础上,在最后插入一个数. 新节 ...
随机推荐
- MySQL错误解决10038
[错误解决]本地计算机上的mysql服务启动停止后,某些服务在未由其他服务或程序使用时将自动停止 标签: mysql计算机 2016-12-01 17:49 5508人阅读 评论(2) 收藏 举报 ...
- MATLAB中的randi函数
randi Pseudorandom integers from a uniform discrete distribution.来自一个均匀离散分布的伪随机整数 R = randi(IMAX,N) ...
- Qt编码设置
1.Qt Creator -> 工具 -> 选项 -> 环境 - >概要 -> 语言 Qt Creator本身界面的语言选择,与cpp文件编码无关,与可执行文件显示 ...
- BZOJ4152 AMPPZ2014 The Captain(最短路)
事实上每次走到横坐标或纵坐标最接近的点一定可以取得最优方案.于是这样连边跑最短路就可以了. #include<iostream> #include<cstdio> #inclu ...
- 什么是Docker?(6-12)
关于什么是Docker,刚开始学的时候一脸懵X,这个东西到底是干嘛用的啊?偶然间在知乎上刷到一个比较通俗的解释: Docker就相当于一个容器,这个容器了不得了,它里面能搭好你项目需要的所有环境,并且 ...
- 【题解】洛谷9月月赛加时赛 —— Never·island
有趣有趣~ヾ(✿゚▽゚)ノ真的很有意思的一道dp题!感觉可以提供很多非常有意思的思路~ 现场打的时候考虑了很久,但并没有做出来,主要还是卡在了两个地方:1.考虑到按照端点来进行dp,但没有办法将两个端 ...
- poj3207 Ikki's Story IV - Panda's Trick 2-sat问题
---题面--- 题意:给定一个圈,m条边(给定),边可以通过外面连,也可以通过里面连,问连完这m条边后,是否可以做到边两两不相交 题解: 将连里面和连外面分别当做一种决策(即每条边都是决策点), 如 ...
- openjudge666:放苹果—题解
(测试这里的markdown,同时也有纪念意义吧--第一次写的题解) 当时刚学递推的时候做的一道题 oj上的666题 666:放苹果 总时间限制: 1000ms 内存限制: 65536kB 描述 把M ...
- Codeforces Round #394 (Div. 2)A水 B暴力 C暴力 D二分 E dfs
A. Dasha and Stairs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- like tp
$where['insurance_order_num'] = array('like',$insurance_order_num.'%'); //右边模糊搜索,2099032902309张三 和 2 ...