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. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  2. UVA - 11488 Hyper Prefix Sets(trie树)

    1.给n个只含0.1的串,求出这些串中前缀的最大和. 例1: 0000 0001 10101 010 结果:6(第1.2串共有000,3+3=6) 例2: 01010010101010101010 1 ...

  3. C语言-1.结构体,2.枚举,3.typedef,4.预处理指令的概念,5.条件编译

    1. 结构体数组 定义:由若干个相同类型的结构体变量组成的有序的集合. 定义格式: 1) 定义结构体的同时定义结构体数组 struct Car{ int lunzi; int speed; }cars ...

  4. 分析函数Ratio_to_report使用

    分析函数Ratio_to_report( ) over()使用说明 表中需要计算单项占比:比如单项在部门占比多少,单项在公司占比多少.特别是在财务单项计算,部门个人薪水计算上. Ratio_to_re ...

  5. net share

    IT知识梳理 2017-11-30 06:57:10 Dos 命令进阶(一)讲解思路 1.Net常用命令 (1)net share - 查看共享命令 net share ipc$ - 设置ipc$共享 ...

  6. ObjectInputStream与ObjectOutputStream类实现对象的存取

    1. ObjectInputStream与ObjectOutputStream类所读写的对象必须实现Serializable接口,对象中的transient和static类型成员变量不会被读取和写入 ...

  7. bzoj 1610: [Usaco2008 Feb]Line连线游戏【瞎搞】

    阴沟翻船.jpg 居然忘了除0的情况 枚举两两之间的线,把斜率装起来排个序去个重就好了 真是水的一晚上呢 #include<iostream> #include<cstdio> ...

  8. layui table 详细讲解

     layui.use('table', function () {             var table = layui.table;             /*第一种原始写法*/       ...

  9. springMVC RedirectAttributes

    @Controller public class TestController { @RequestMapping("/redirectDemo") public String r ...

  10. CF449D Jzzhu and Numbers

    题解 刚刚学习了高维前缀和 这道题就肥肠简单了 高维前缀和其实原理肥肠简单 就是每次只考虑一维,然后只做这一维的前缀和 最后求出的就是总前缀和了 那么对于这道题 也就很简单了 发现选择的所有数每一位都 ...