题目:

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

  1. The root is the maximum number in the array.
  2. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  3. The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.

Construct the maximum tree by the given array and output the root node of this tree.

Example 1:

Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree: 6
/ \
3 5
\ /
2 0
\
1

Note:

  1. The size of the given array will be in the range [1,1000].

分析:

给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:

  1. 二叉树的根是数组中的最大元素。
  2. 左子树是通过数组中最大值左边部分构造出的最大二叉树。
  3. 右子树是通过数组中最大值右边部分构造出的最大二叉树。

通过给定的数组构建最大二叉树,并且输出这个树的根节点。

每次选出数组中的最大值作为根节点,左子树就递归执行最大值的左侧,右子树递归执行最大值的右侧。当数组为空的时候返回空指针。

程序:

/**
* 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* constructMaximumBinaryTree(vector<int>& nums) {
if(nums.size() == ) return nullptr;
auto it = max_element(nums.begin(), nums.end());
TreeNode* root = new TreeNode(*it);
vector<int> l(nums.begin(), it);
vector<int> r(it+, nums.end());
root->left = constructMaximumBinaryTree(l);
root->right = constructMaximumBinaryTree(r);
return root;
}
};

LeetCode 654. Maximum Binary Tree最大二叉树 (C++)的更多相关文章

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

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

  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最大二叉树

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

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

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

  5. 654. Maximum Binary Tree

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

  6. [Leetcode Week14]Maximum Binary Tree

    Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...

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

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

  8. 【leetcode】654. Maximum Binary Tree

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

  9. [LeetCode] 655. Print Binary Tree 打印二叉树

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

随机推荐

  1. CF1195E OpenStreetMap

    题目链接 题意 有一个\(n\times m\)的矩阵,询问其中所有大小为\(a \times b\)的子矩阵的最小值之和. \(1\le n,m \le 3000\) 思路 因为是子矩阵的大小是固定 ...

  2. linux 硬盘满了后,查看使用目录占用空间情况

    cd 切换到目录, du -ah --max-depth=1 查看当前目录下的 文件夹 占用情况

  3. springboot2.0 management.security.enabled无效

    在1.5.x版本中通过management.security.enabled=false来暴露所有端点 在使用springcloud的时候,如果基于springboot2的版本的配置中心,无法使用SV ...

  4. gradle 多模块Springboot项目 compile project引用其他模块的坑

    本来以为子项目中compile project(':xxx'),就能引用其他模块了,因为之后idea也没在引用时候标红 然而我gradle build的时候,居然各种找不到引用模块的类 最后在stac ...

  5. sql语句规范参考

    公司有SQL语句规范的参考,这里特别做个笔记. 书写风格 1. 语句关键字应全部使用小写. 2. 引用字符时应使用单引号.如:update testable set idcol=’abcd’. 3. ...

  6. Visual Studio 2019 (VS2019)正式版安装 VisualSVN Server 插件

    VS2019 正式版最近刚刚推出来,目前 Ankhsvn 还不支持,它最高只支持 VS2017,全网搜索了一下,也没有找到.在 Stackoverflow 上看了一下,找到这篇问答: 自己按照这种方法 ...

  7. ASP.NET web.config 配置里部分参数详细说明

    Session配置 <!-- <identity impersonate = "false" [true|false] userName = "" ...

  8. 微信分享网页时自定义缩略图和简介(.net版本)

    要实现微信分享网页时自定义缩略图和简介,需开发者在公众平台网站中创建公众号.获取接口权限后,通过微信JS-SDK的分享接口,来实现微信分享功能. 下面来说明实现步骤. 第一部分 准备步骤 步骤一:注册 ...

  9. Jar 打包与执行

    Java学习笔记之一,用于个人记录.整理自<Head First Java>. 假设有如下目录结构: 程序入口在 Jukebox8.java.这个代码文件开头是有如下这样的包声明语句的: ...

  10. Winform的控件以及DataGridView的一般使用

    先上学习测试的一些截图 1:获取多个控件上面的值(checkbox,combobox,textbox,radiobutton) 2:获取到选择行的主键ID的value,方便我们进一步CURD 3:获取 ...