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 ...
随机推荐
- 建立自己的私有docker(ssl&login auth)
建立私有docker需要先建立ssl证书,然后建立htpass的登陆证书 最后使用nginx配置docker-compose.yml 参考: https://www.digitalocean.com/ ...
- Masonry整体动画更新约束
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...
- NSArray, NSSet, NSDictionary
一.Foundation framework中用于收集cocoa对象(NSObject对象)的三种集合分别是: NSArray 用于对象有序集合(数组)NSSet 用于对象无序集合(集合) NSDic ...
- 二:网络--GET请求和POST请求
一.GET请求和POST请求简单说明 GET - 从指定的服务器中获取数据 POST - 提交数据给指定的服务器处理 GET方法: 使用GET方法时,查询字符串(键值对)被附加在URL地址后面一起发送 ...
- 《Spring Boot官方指南》(二)入门
如果您刚开始接触Spring Boot,或者简称’Spring’, 这个部分对您来说非常有用.这个部分提供了“是什么?”,“怎么做?”和 “为什么?”的解释.您除了会阅读一份附带Spring Boot ...
- EF include 预先加载
在asp.net mvc 中,常在控制器中预先加载导航属性,以便在视图中能够显示起关联的数据. 如果不预先加载,View中就会无法呈现外键的 关联数据. 会提示EF 错误发生. 一. 模型: publ ...
- WinHTTrack
看过<大湿教我写.net通用权限框架(1)之菜单导航篇>之后发生的事 http://www.cnblogs.com/wolf-sun/p/3436585.html 用此工具下载别人整站的图 ...
- #if、#ifdef、#if defined之间的区别(转载)
转自:http://www.yucoat.com/c_program/difference_if_ifdef_if_defined.html #if的使用说明 #if的后面接的是表达式 #if (MA ...
- jetbrains软件使用
1.注册(有条件的话建议使用官方渠道注册https://www.jetbrains.com/store) 2018 license server http://idea.congm.in 2.禁用双击 ...
- TransposonPSI——转座子分析的入门自学
最近需要做转座子分析,查找发现可以使用 TransposonPSI 来进行分析.但是登陆官网,该软件 update 时间为 2013 年,但是因为时间紧迫,暂时还没有进行其他方法的调研,所以先选用该软 ...