leetcode_654. Maximum Binary Tree
https://leetcode.com/problems/maximum-binary-tree/
给定数组A,假设A[i]为数组最大值,创建根节点将其值赋为A[i],然后递归地用A[0,i-1]创建左子树,用A[i+1,n]创建右子树。
使用vector的assign函数,该函数的特性:
Any elements held in the container before the call are destroyed and replaced by newly constructed elements (no assignments of elements take place).
This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.
class Solution
{
public: TreeNode* constructMaximumBinaryTree(vector<int>& nums)
{
vector<int>::iterator maxiter,iter=nums.begin();
int maxn=INT_MIN,len=nums.size();
while(iter!=nums.end())
{
if(*iter>maxn)
{
maxn=*iter;
maxiter=iter;
}
iter++;
}
vector<int> lnums,rnums;
TreeNode *node = new TreeNode(maxn);
if(maxiter!=nums.begin())
{
lnums.assign(nums.begin(),maxiter);
node->left = constructMaximumBinaryTree(lnums);
}
if(maxiter!=nums.end()-)
{
rnums.assign(maxiter+,nums.end());
node->right = constructMaximumBinaryTree(rnums);
}
return node;
}
};
因为assign函数的特性是,每次给vector赋值都会自动销毁原先vector中的对象,虽然这里使用的是c++内置类型,但是多少还是会有开销。所以又写了一个不使用assign的版本。
class Solution
{
public: TreeNode* constructMaximumBinaryTree(vector<int>& nums)
{
return buildTree(nums, , nums.size()-);
}
TreeNode* buildTree(vector<int>& nums, int l, int r)
{
if(l > r)
return NULL;
if(l == r)
{
TreeNode* node = new TreeNode(nums[l]);
return node;
}
int max_n = INT_MIN, max_index = l;
for(int i=l; i<=r ; i++)
if(nums[i]>max_n)
{
max_n = nums[i];
max_index = i;
}
TreeNode* root = new TreeNode(max_n);
root->left = buildTree(nums, l, max_index-);
root->right = buildTree(nums, max_index+, r);
return root;
}
};
leetcode_654. Maximum Binary Tree的更多相关文章
- 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_998. Maximum Binary Tree II
https://leetcode.com/problems/maximum-binary-tree-ii/ 在654. Maximum Binary Tree版本的建树基础上,在最后插入一个数. 新节 ...
- 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 - 654. Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- [Swift]LeetCode998. 最大二叉树 II | Maximum Binary Tree II
We are given the root node of a maximum tree: a tree where every node has a value greater than any o ...
- 654. Maximum Binary Tree 最大节点劈开,然后左边、右边排序
[抄题]: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
随机推荐
- Deep Learning 27:Batch normalization理解——读论文“Batch normalization: Accelerating deep network training by reducing internal covariate shift ”——ICML 2015
这篇经典论文,甚至可以说是2015年最牛的一篇论文,早就有很多人解读,不需要自己着摸,但是看了论文原文Batch normalization: Accelerating deep network tr ...
- HTML5中meta属性
meta属性在HTML中占据了很重要的位置.如:针对搜索引擎的SEO,文档的字符编码,设置刷新缓存等.虽然一些网页可能没有使用meta,但是作为正规军,我们还是有必要了解一些meta的属性,并且能够熟 ...
- java语法基础(二)
流程控制语句 表达式语句 在表达式后面添加:就构成了表达式语句,简称“语句” 我们编写java代码,更多时候都是在书写表达式语句. int i;声明语句 i = 10;赋值语句 流程控制语句 流程控制 ...
- 在Main Thread中使用异步
Whenever you first start an Android application, a thread called "main" is automatically c ...
- linux CANopenSocket 初试
/************************************************************************************** * linux CANo ...
- I.MX6 su.c 测试
/************************************************************************* * I.MX6 su.c 测试 * 说明: * 今 ...
- I.MX6 mkuserimg.sh hacking
/*********************************************************************** * I.MX6 mkuserimg.sh hackin ...
- 去除inline-block的间隙
产生间隙的原因就是标签之间的空格,去除的方法: 1 设置父元素的font-size:0;空格字符的宽高都为0, <div class="demo1 demo2"> &l ...
- 腾讯API
相关文档: API列表 腾讯开放平台联调工具集 公共返回码说明 SDK下载
- asp.net MVC4 学习(一)
asp.net MVC 回顾 Html.ActionLink http://www.cnblogs.com/jiagoushi/p/3905828.html 选择基本模板,视图引擎 选择Razor A ...