654. Maximum Binary Tree最大二叉树
网址: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最大二叉树的更多相关文章
- LeetCode 654. Maximum Binary Tree最大二叉树 (C++)
题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...
- [LeetCode] 654. Maximum Binary Tree 最大二叉树
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- 654. Maximum Binary Tree
654. Maximum Binary Tree 题目大意: 意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步. 读完就知道是递归了. 这个题真尼 ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- LeetCode - 654. Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- 654. Maximum Binary Tree 最大节点劈开,然后左边、右边排序
[抄题]: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
- 【leetcode】654. Maximum Binary Tree
题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
- [LeetCode]654. Maximum Binary Tree最大堆二叉树
每次找到数组中的最大值,然后递归的构建左右树 public TreeNode constructMaximumBinaryTree(int[] nums) { if (nums.length==0) ...
- [LeetCode] Maximum Binary Tree 最大二叉树
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
随机推荐
- IDEA 创建Web项目
1,创建Project:依次点击File–new Project: 创建.png 2,选择Empty Project项目,点击Next: 下一步 3,输入项目名称,选择项目路径: Past ...
- JDK8到JDK12各个版本的重要特性整理
JDK8新特性 1.Lambda表达式 2.函数式编程 3.接口可以添加默认方法和静态方法,也就是定义不需要实现类实现的方法 4.方法引用 5.重复注解,同一个注解可以使用多次 6.引入Optiona ...
- Spring Boot中使用Swagger2构建RESTful APIs
关于 Swagger Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. S ...
- ASP.NET 教程汇总
channel9 https://channel9.msdn.com/ .net core项目实战 https://study.163.com/course/introduction.htm?cour ...
- mysql之统一刷表
统一刷表: update report set pdfPath= CONCAT(pdfPath ,substring_index(fileLink, '\\', -1)); update report ...
- Linux中LAMP构架的实现
LAMP:Linux+Apache+Mysql+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度共同组 ...
- 外网登录访问树莓派 Raspberry Pi
外网登录访问树莓派 Raspberry Pi 本地的树莓派 Raspberry Pi,只能在局域网内访问,怎样从公网也能登录访问树莓派 Raspberry Pi? 本文将介绍具体的实现步骤. 1. 准 ...
- Java的流
Java中的流主要是用来读写文件的.只要明确你是读文件还是写文件就可以确定使用哪种流了.读:read 写:writer 1.读文件:filereader这个类,顾名思义就知道是读文件了. 2.写文 ...
- Linux环境下部署开源版“禅道”方法
1.开源版安装包下载(Linux系统版本查看命令 uname -a) 32位 [root@iZbp~]# wget http://dl.cnezsoft.com/zentao/9.0.1/ZenTao ...
- 系统管理--配置Gitlab
很多教程都有配这个,但这个又不能用于”源码管理”模块拉取代码,我一直很困惑这个配置有什么用,然后就找到了该插件的github项目地址才弄明白,链接:https://github.com/jenkins ...