网址: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. awk if print

    awk  -F","  '{ if($4=="abc" && $5=="def"){print $1} else {prin ...

  2. redis php操作命令

    redis的五种存储类型的具体用法 String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者 ...

  3. 命令行创建cocos2d-x的工程

    1. 命令行创建cocos lua工程cocos new MyGame -p com.your_company.mygame -l lua2. 进入工程目录, 编译运行时库cocos compile ...

  4. docker运行镜像报错:"write init-p: broken pipe"

    docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting cont ...

  5. linux 定时计划任务设置

    安装 crontabs服务并设置开机自启 yum install crontabs systemctl enable crond (设为开机启动) systemctl start crond(启动cr ...

  6. opencv学习之路(34)、SIFT特征匹配(二)

    一.特征匹配简介 二.暴力匹配 1.nth_element筛选 #include "opencv2/opencv.hpp" #include <opencv2/nonfree ...

  7. ipv4网络无访问权限

    昨天折腾了一天这个网络受限问题,都没有得到解决,驱动精灵.电脑管家(我不用360).重置Winsock目录.网络重置.组策略控制台.重启路由器(学校的以太网,工作时间不好重置路由器)都用了个遍,一直都 ...

  8. C# Winform ComBox三种赋值方式

    https://www.cnblogs.com/ingstyle/p/4815303.html 第一种方法: DataTable dt = new DataTable(); dt.Columns.Ad ...

  9. swift 有道 翻译文档(2 条件语句 循环语句)

    控制流使用if和switch来创建条件语句,使用for-in.while和repeat-while来创建循环.条件或循环变量的括号是可选的.身体周围需要支撑. let individualScores ...

  10. vue cli 3 lintOnSave 配置有时无效问题

    一个使用vue cli 3.2创建的项目,创建时未开启 lintOnSave,后来希望开启并设置为 lintOnSave: 'error',但配置不生效. 解决方法1:新创建项目(此时vue cli ...