leetcode_654. Maximum Binary Tree

news/2024/7/3 8:39:05

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()-1)
        {
            rnums.assign(maxiter+1,nums.end());
            node->right = constructMaximumBinaryTree(rnums);
        }
        return node;
    }
};

 

因为assign函数的特性是,每次给vector赋值都会自动销毁原先vector中的对象,虽然这里使用的是c++内置类型,但是多少还是会有开销。所以又写了一个不使用assign的版本。

class Solution
{
public:

    TreeNode* constructMaximumBinaryTree(vector<int>& nums)
    {
        return buildTree(nums, 0, nums.size()-1);
    }
    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-1);
        root->right = buildTree(nums, max_index+1, r);
        return root;
    }
};

 

转载于:https://www.cnblogs.com/jasonlixuetao/p/10582782.html


http://www.niftyadmin.cn/n/4820377.html

相关文章

【vueJs源码】阅读之vm.$watch函数

我们经常使用watch肯定知道它&#xff0c;他和computer一样都是数据发生变化都会触发它。今天我们就来了解一下它的原理。 他的用法 Vue.prototype.$watch function (expOrFn: string | (() > any),cb: any,options?: Record<string, any> ): Function这是vuejs源…

Common Lisp

Common Lisp 来自 维客 Jump to: navigation, searchCommon Lisp&#xff0c;一般缩写为 CL&#xff08;不要和缩写同为CL的組合邏輯混淆&#xff09;&#xff0c;是Lisp的方言&#xff0c;标准由ANSI X3.226-1994定义。它是为了标准化此前众多的Lisp分支而开发的&#xff0c;它…

qa qc qm的区别

稍后更新。。。转载于:https://www.cnblogs.com/Chamberlain/p/10586562.html

python --web服务器

用python建立简单的web服务器 利用Python自带的包可以建立简单的web服务器。 在DOS里cd到准备做服务器根目录的路径下&#xff0c;输入命令&#xff1a; python -m Web服务器模块 [端口号&#xff0c;默认8000] 例如&#xff1a; python -m SimpleHTTPServer 8080 然后就…

几个免费的Scheme(Lisp)解释器

几个免费的Scheme&#xff08;Lisp&#xff09;解释器关键字: lisp schemeLisp 是一个古老的函数式编程语言&#xff0c;Scheme则起源于MIT的一种Lisp方言。当前编程语言的一些特性&#xff0c;如尾递归、匿名函数、动态改变代码的功能等等&#xff0c;不 少是受到了Lisp的启发…

终极秘密---------windows里藏着9.11的惊天大密码

终极秘密---------windows里藏着9.11的惊天大密码 神秘连锁"密码"泄漏恐怖分子袭美玄机 方法:用WORD~~~编辑文档输入Q33NY(必须大写) (这是9.11撞击世界贸易中心的沙特勇士们乘坐的航班号); 第三,将字体大小改到72; 最后,将字体转成 wingdings字体.你看到…

C# 印刷文字识别-身份证识别

1.阿里免费提供500次识别。需要购买该产品。 2.产品购买成功后&#xff0c;获取appcode值&#xff0c;这个要放代码中的appcode中。 3.识别的图片路径 public static void Main(string[] args){//身份证识别// Console.WriteLine("Hello World!");String url "…

Linux上的集成开发环境收藏

Linux上的集成开发环境收藏 新一篇: Linux下C语言编程基础知识 | 旧一篇: 实现千万级数据分页的存储过程&#xff01; <script>function StorePage(){ddocument;td.selection?(d.selection.type!None?d.selection.createRange().text:):(d.getSelection?d.getSelectio…