网址:https://leetcode.com/problems/maximum-binary-tree/

参考: https://leetcode.com/problems/maximum-binary-tree/discuss/106146/C%2B%2B-O(N)-solution

我自己的做法是依照题意的逻辑,逐一递归的做法。

O(n2)

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* getNode(vector<int>& nums)
{
TreeNode* res = new TreeNode();
int maxx = nums[];
int maxw = ;
// 遍历出最大值和其下标
for(int i=; i<nums.size(); i++)
{
if(maxx < nums[i])
{
maxx = nums[i];
maxw = i;
}
}
res->val = maxx; // 开始左右节点的构造
// 最大节点是否位于片段的左端
if(nums.begin() == nums.begin()+maxw)
{
res->left = NULL;
}
else
{
// 构造vector片段,继续递归
vector<int> nums_left(nums.begin(), nums.begin()+maxw);
res->left = getNode(nums_left);
}
// 最大节点是否位于片段的右端
if(nums.begin()+maxw+ == nums.end())
{
res->right = NULL;
}
else
{
// 同理
vector<int> nums_right(nums.begin()+maxw+, nums.end());
res->right = getNode(nums_right);
}
return res;
} TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
TreeNode* ans = getNode(nums);
return ans;
}
};

在discuss中发现有O(N)的做法!

654. Maximum Binary Tree最大二叉树的更多相关文章

  1. LeetCode 654. Maximum Binary Tree最大二叉树 (C++)

    题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...

  2. [LeetCode] 654. Maximum Binary Tree 最大二叉树

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

  3. 654. Maximum Binary Tree

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

  4. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  5. LeetCode - 654. Maximum Binary Tree

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

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

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

  7. 【leetcode】654. Maximum Binary Tree

    题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...

  8. [LeetCode]654. Maximum Binary Tree最大堆二叉树

    每次找到数组中的最大值,然后递归的构建左右树 public TreeNode constructMaximumBinaryTree(int[] nums) { if (nums.length==0) ...

  9. [LeetCode] Maximum Binary Tree 最大二叉树

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

随机推荐

  1. 字王大藏经体v0.1概念版

    字王大藏经体v0.1概念版 zw-dzj 字王大藏经体v0.1概念版,是字王<中华大字库>2018版升级过程当中,在Github搜索资源时的意外惊喜. 大藏经为佛教经典的总集,简称为藏经. ...

  2. Lua 哑变量

    [1]哑变量 哑变量,又称为虚拟变量.名义变量. 还得理解汉语的博大精深,‘虚拟’.‘名义’.‘哑’等等,都是没有实际意义.所以,哑变量即没有现实意义的变量. 哑变量的应用示例如下: local fi ...

  3. SQL Server 复制表结构以及数据,去除表中重复字段

    --复制另一个数据库中的某张表的结构及数据--select * from Test.dbo.TestTable(查询表中所有数据) --into [表名] 插入当前数据库新表,如果没有该表就创建 se ...

  4. php 版本升高后 会出现 之Deprecated: Function ereg_replace() is deprecated的解决方法

    这个问题是因为php版本过高. 在php5.3中,正则函数ereg_replace已经废弃,而dedecms还继续用.有两个方案可以解决以上问题: 1.把php版本换到v5.3下. 2.继续使用v5. ...

  5. rabbitMq及安装、fanout交换机-分发(发布/订阅)

    <dependency>            <groupId>com.rabbitmq</groupId>            <artifactId& ...

  6. Springboot 实现多环境配置

    多环境配置 我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测试.生产等.其中每个环境的数据库地址.服务器端口等等配置都会不同,如果在为不同环境打包时 ...

  7. ss命令详解

    ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息 ...

  8. mysql之统一刷表

    统一刷表: update report set pdfPath= CONCAT(pdfPath ,substring_index(fileLink, '\\', -1)); update report ...

  9. 【ubuntu】-桌面假死的解决办法

    第一,通过ctrl+art+F1(1-6),启动本地终端 切换到了字符界面tty1 第二,查询进程,ps -e |grep tty7 得到tty7的pid号 第三,杀死tty7的进程 , kill 9 ...

  10. WIN10下微信崩溃(已经是最新版)的解决方法

    微信运行错误---------------------------你的微信崩溃次数较多,建议使用最新版本,点击"确定"到官网(http://pc.weixin.qq.com/)下载 ...