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 ...
随机推荐
- redis 字符串(string)函数
字符串(string)函数 get 命令/方法/函数 Description Get the value related to the specified key 取得与指定的键值相关联的值 Para ...
- Kindle:自动追更之Calibre2脚本
#!/usr/bin/env python2 # vim:fileencoding=utf-8 from __future__ import unicode_literals, division, a ...
- final关键字的几种用法
在java的关键字中,static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.下面我们来了解一下final ...
- tcp ip 协议安全
ARP(地址解析协议) 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求 ...
- python python中那些双下划线开头的那些函数都是干啥用用的
1.写在前面 今天遇到了__slots__,,所以我就想了解下python中那些双下划线开头的那些函数都是干啥用用的,翻到了下面这篇博客,看着很全面,我只了解其中的一部分,还不敢乱下定义. 其实如果足 ...
- GO函数
函数定义 Go语言中定义函数使用func关键字. func 函数名(参数)(返回值){ 函数体 } 函数名:由字母.数字.下划线组成.但函数名的第一个字母不能是数字.在同一个包内,函数名也称不能重名( ...
- 【记录】VMware解决网络找不到服务器的问题
本想在虚拟机上的Linux上练习安装Mysql8.0版本的,网络连不上的问题卡了N天简直 1. 点击虚拟机右键设置,虚拟机默认设置为NAT模式,这里无需修改. 2. 点击编辑,虚拟网络设置,勾选主机连 ...
- JS对象格式化方法:pretty_format
/* * 格式化 * */ var pretty_format = function (obj, indent) { if (obj === null) return 'null'; if (obj ...
- source insight 4.0.86.0 安装过程中出现的问题
1.sourceinsight_4.0.86.0-setup.exe 2.sourceinsight4.exe覆盖安装目录中的sourceinsight4.exe 3.导入lic文件 过程中360会将 ...
- ActiveMQ下载与安装(Linux环境下进行)
下载 官方网站:http:activemq.apache.org/ 安装(liunx) 1.将apache-activemq-5.12.0-bin.tar.gz(liunx环境下的压缩包)上传至服务器 ...