网址: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. 使用gulp构建一个项目

    gulp是前端开发过程中自动构建项目的工具,相同作用的还有grunt.构建工具依靠插件能够自动监测文件变化以及完成js/sass/less/html/image/css/coffee等文件的语法检查. ...

  2. requests库

    还没整理,先贴俩链接. https://www.cnblogs.com/lilinwei340/p/6417689.html http://docs.python-requests.org/zh_CN ...

  3. java爬取免费HTTP代理 code-for-fun

    偶然看到一个提供免费HTTP 代理IP的网站,该网站一两个小时就会更新一次,很有用.之后自己就用Java写了一个爬虫,爬取网站上的代理IP,以备后用. 网站源码: <!DOCTYPE html& ...

  4. qemu到kvm的处理,再到vm的运行

    1.QEMU创建虚拟机发起:kvm_ioctl(s, KVM_CREATE_VM, type); KVM中kvm_dev_ioctl判断参数->kvm_dev_ioctl_create_vm-& ...

  5. Why choose Nexiq USB-link 125032 Diesel Truck Diagnose

    Nexiq 125032 usb link is Diesel Truck diagnostic Interface. Nexiq truck scanner can compatible with ...

  6. 【源码】HashMap源码及线程非安全分析

    最近工作不是太忙,准备再读读一些源码,想来想去,还是先从JDK的源码读起吧,毕竟很久不去读了,很多东西都生疏了.当然,还是先从炙手可热的HashMap,每次读都会有一些收获.当然,JDK8对HashM ...

  7. Qt重绘之update,repaint详解

    Qt里面的重绘和Windows编程里面的重绘差不多.但是Qt的重绘更有特色,更加智能. 在讲之前,先说说paintEvent() paintEvent()是一个虚函数槽(slot),子类可以对父类的p ...

  8. Tomcat在Window控制台下启动时乱码的两种解决办法

    在命令提示符中启动Tomcat时,日志窗口出现乱码: 乱码的原因肯定是日志解码错误引起的,因此就有一系列问题: 1.这个窗口的文本编码是什么? 窗口的文本编码查看:右击窗口>选项 可以看到窗口的 ...

  9. Bootstrap各种进度条的实例讲解

    本章将讲解 Bootstrap 进度条.在本教程中,您将看到如何使用bootstrap教程.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Interne ...

  10. Dockerfile详解及优化

    Dockerfile详解 0. Dockerfile的作用 docker可以根据Dockerfile中的指令来构建docker镜像.Dockerfile是一个文本文件,其应当包含用户想要构建一个镜像的 ...