Maximum Binary Tree 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/maximum-binary-tree/description/


Description

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

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

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

Solution

class Solution {
public:
TreeNode* getSubTree(vector<int>& nums, int start, int end) {
TreeNode* resultNode;
if (start == end) {
resultNode = new TreeNode(nums[start]);
return resultNode;
}
int maxIdx = start;
int i;
for (i = start; i <= end; i++) {
if (nums[i] > nums[maxIdx])
maxIdx = i;
}
resultNode = new TreeNode(nums[maxIdx]);
if (maxIdx > start) {
resultNode -> left = getSubTree(nums, start, maxIdx - 1);
}
if (maxIdx < end) {
resultNode -> right = getSubTree(nums, maxIdx + 1, end);
}
return resultNode;
} TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
if (nums.empty())
return NULL;
return getSubTree(nums, 0, nums.size() - 1);
}
};

解题描述

这道题的题意是,对给定的一个数组,构造一棵所谓的“最大二叉树”。很容易想到的就是使用递归的思想,每次都对数组的一段进行处理,找出数组段中最大的元素,将该元素所谓当前树的树根,对元素左右两边两个数组段分别构造“最大二叉树”,分别作为树根的左子树和右子树。

[Leetcode Week14]Maximum Binary Tree的更多相关文章

  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 Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  3. LeetCode 654. Maximum Binary Tree最大二叉树 (C++)

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

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

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

  5. LeetCode题解Maximum Binary Tree

    1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...

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

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

  7. Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)

    Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...

  8. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  9. leetcode_998. Maximum Binary Tree II

    https://leetcode.com/problems/maximum-binary-tree-ii/ 在654. Maximum Binary Tree版本的建树基础上,在最后插入一个数. 新节 ...

随机推荐

  1. arp_annouce=2详解

    arp_annouce=0的时候 手册上说的是到底是是说当我这个包出去的时候询问arp地址, 当arp_announce=0的时候,是说使用数据包中的地址去进行arp的请求, 当arp_announc ...

  2. activeMQ 讲解及实战

    #### 软件架构项目中需要用到activeMQ 下载地址:http://activemq.apache.org/download.html #### 安装教程需要安装jdk环境activeMQ免安装 ...

  3. linux 安装 bitnamid-redmine

    Unix 和 Linux 安装 Perl Unix/Linux 系统上 Perl 安装步骤如下: 通过浏览器打开 http://www.perl.org/get.html. 下载适用于 Unix/Li ...

  4. 【bzoj5094】硬盘检测 乱搞

    题目描述 已知从 $n$ 个不同的32位无符号整数中随机选 $m=10000$ 次所得的结果,求可能性最大的 $n$ ,其中 $n=10^k,1\le k\le 7$. 输入 第一行包含一个正整数m( ...

  5. 关于"作数类型冲突: nvarchar 与 image 不兼容"的问题

    数据库如果是Image类型,当执行插入语句时,如果插入的值是DBNull.Value时提示:操作数类型冲突: nvarchar 与 image 不兼容; 出现这个问题的原因是没有指定DbType的原因 ...

  6. CentOS 文件隐藏属性

    1.chattr用于配置文件的隐藏属性 语法: chattr [-RVf] [-+=aAcCdDeijsStTu] [-v version] files... 选项与参数: +:增加某个特殊参数,其他 ...

  7. 【原创】宿主机远程登录虚拟机(windows server 2003系统)

    转载请注明,谢谢合作 1.虚拟机网络设置为  “桥接模式”如图 2.系统装好并登陆后 右键点击我的电脑,点击属性,然后在弹出来的选择框中勾选远程桌面-->启用这台计算机的远程桌面 然后点添加-- ...

  8. 什么是Docker?(6-12)

    关于什么是Docker,刚开始学的时候一脸懵X,这个东西到底是干嘛用的啊?偶然间在知乎上刷到一个比较通俗的解释: Docker就相当于一个容器,这个容器了不得了,它里面能搭好你项目需要的所有环境,并且 ...

  9. Jquery常用正则验证

    常用校验的正则表达式var rulesConfig = { /** * str.replace(/^\s+|\s+$/g, '') 解析: str:要替换的字符串 \s : 表示 space ,空格 ...

  10. BZOJ5286:[HNOI/AHOI2018]转盘——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5286 https://www.luogu.org/problemnew/show/P4425 ht ...