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 ...
随机推荐
- HTTP要点概述:四,HTTP方法
使用HTTP协议的时候,客户端可以通过HTTP方法告知服务器自己请求的意图. 看了这篇文章以后,谁再说HTTP方法只有GET和POST,你的眼睛是用来吃饭的嘛! 一,GET:获取资源 GET用来请求访 ...
- java定时器2-spring实现
spring定时器(基于xml) spring定时器(基于注解) quartz定时器 1.使用基于xml配置的spring定时器 首先编写定时任务类Mytask public class Mytask ...
- Oracle - 数据更新 - 增删改
/* 数据的更新 增加 删除 修改 */ -----------------------------------增加(一次只能插入一条数据) --自定义插入数据列的顺序 ,,); --按照数据库默认的 ...
- return value, output parameter,
Return Value https://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql?view=s ...
- casperjs在拆分文件后的中文乱码问题的解决
windows环境. capserjs的中文乱码使用phantom.outputEncoding="GBK";即可解决. 但当我们脚本很大,需要拆分时(参考http://docs. ...
- YTU 2907: 类重载实现矩阵加法
2907: 类重载实现矩阵加法 时间限制: 1 Sec 内存限制: 128 MB 提交: 345 解决: 164 题目描述 编写矩阵类Matrix,实现两个2x3矩阵相加.主函数已给定. 输入 两 ...
- Android中onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent的调用顺序及内部原理
在Android中需要经常对用户手势进行判断,在判断手势时需要精细的分清楚每个触摸事件以及每个View对事件的接收情况,在View,ViewGroup,Activity中都可以接收事件,在对事件进行处 ...
- lucene DocValues——本质是为通过docID查找某field的值
什么是docValues? docValues是一种记录doc字段值的一种形式,在例如在结果排序和统计Facet查询时,需要通过docid取字段值的场景下是非常高效的. 为什么要使用docValues ...
- HttpWebRequest以及HttpWebResponse
上次介绍了用WebClient的方式提交POST请求,这次,我继续来介绍用其它一种方式 HttpWebRequest以及HttpWebResponse 自认为与上次介绍的WebClient最大的不同之 ...
- SPOJ:Labyrinth(最大直线)
The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...