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的更多相关文章

  1. 654. Maximum Binary Tree

    654. Maximum Binary Tree 题目大意: 意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步. 读完就知道是递归了. 这个题真尼 ...

  2. [Leetcode Week14]Maximum Binary Tree

    Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...

  3. leetcode_998. Maximum Binary Tree II

    https://leetcode.com/problems/maximum-binary-tree-ii/ 在654. Maximum Binary Tree版本的建树基础上,在最后插入一个数. 新节 ...

  4. Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)

    Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...

  5. 【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 ...

  6. LeetCode - 654. Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  7. [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  8. [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 ...

  9. 654. Maximum Binary Tree 最大节点劈开,然后左边、右边排序

    [抄题]: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...

随机推荐

  1. apache配置访问限制

    1.禁止访问某些文件/目录 增加Files选项来控制,比如要不允许访问 .txt扩展名的文件,保护php类库: <Files ~ "\.txt$"> Order all ...

  2. maven目录结构介绍篇

    bin  该目录包含了mvn运行的脚本,这些脚本用来配置java命令,准备好classpath喝相关的java系统属性 mvn是基于UNIX平台shell脚本,mvn.bat是基于Windows平台的 ...

  3. Android ConstraintLayout的基本使用

    升级Android studio到2.3版本之后,发现新建Activity或fragment时,xml布局默认布局由RelativeLayout更改为ConstraintLayout了,既然已经推荐使 ...

  4. ios app 上架AppStore

    一.证书的导出      1.1 前期工作        首先你需要有一个苹果的开发者帐号,一个Mac系统.        如果没有帐号可以在打开http://developer.apple.com/ ...

  5. C# mouse keyboard monitor

    /*********************************************************************************** * C# mouse keyb ...

  6. Excel -- 实用技巧

    一起来学习这45个职场Excel小窍门,轻轻松松提高工作效率,超实用,新技能get! 1.快速填充公式的三种方式 2.最快求和 3.添加文字下面线条的2种方法 4.设置列宽的3种方法 5.以cm为单位 ...

  7. bzoj 2733: [HNOI2012]永无乡【并查集+权值线段树】

    bzoj上数组开大会T-- 本来想用set瞎搞的,想了想发现不行 总之就是并查集,每个点开一个动态开点的权值线段树,然后合并的时候把值并在根上,询问的时候找出在根的线段树里找出k小值,看看这个值属于哪 ...

  8. AOP日志框架实现

    AOP日志框架实现 JDK动态代理实现日志框架 首先,在项目包com.ay.test 下创建业务接口类BusinessClassService,具体代码如下: BusinessC lassServic ...

  9. SQL 初级教程学习(三)

    1.SQL JOIN  NNER JOIN:如果表中有至少一个匹配,则返回行 LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行 RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的 ...

  10. 加密解密(3)Bob到CA申请证书过程

    网络安全中最知名的人物大概就是Bob和Alice了,因为很多安全原理阐述中都用这两个虚拟人物来进行实例说明. 我们来看看Bob是怎么从CA中心获得一个数字证书的: 1.Bob首先创建他自己的密钥对(k ...