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. Stax解析XML示例代码

    package org.itat.stax; import java.io.IOException; import java.io.InputStream; import javax.xml.pars ...

  2. RT-thread finsh组件工作流程

    finsh是RT-Thread的命令行外壳(shell),提供一套供用户在命令行的操作接口,主要用于调试.查看系统信息.在大部分嵌入式系统中,一般开发调试都使用硬件调试器和printf日志打印,在有些 ...

  3. jzoj3865[JSOI2014]士兵部署

    ‘ 数据范围:n,m<=10^5,传送门:https://jzoj.net/senior/#main/show/3865 感觉jzoj好高明啊,就是访问不太稳定. 首先题意中被n个点控制的区域相 ...

  4. 【bzoj5089】最大连续子段和 分块+单调栈维护凸包

    题目描述 给出一个长度为 n 的序列,要求支持如下两种操作: A  l  r  x :将 [l,r] 区间内的所有数加上 x : Q  l  r : 询问 [l,r] 区间的最大连续子段和. 其中,一 ...

  5. [洛谷P4092][HEOI2016/TJOI2016]树

    题目大意:给你一棵树,有两个操作: $C\;x:$给第$x$个节点打上标记 $Q\;x:$询问第$x$个节点的祖先中最近的打过标记的点(自己也是自己的祖先) 题解:树剖,可以维护区间或,然后若一段区间 ...

  6. POJ3180:The Cow Prom——题解

    http://poj.org/problem?id=3180 英文题以后都不粘贴题面. 大意:求点数大于1的强连通分量个数 #include<stack> #include<cstd ...

  7. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  8. "HK"日常之冻结术

    在那遥远的MSDN上,有那么一只被隐藏的函数,它掌管着Windows内核威力不容小觑~ 本教程仅作为学习研究,禁止其他用途! 富强.民主.文明.和谐, 自由.平等.公正.法治, 爱国.敬业.诚信.友善 ...

  9. Eclipse NDK 打印LOG信息(都在jni目录下操作)

    http://blog.csdn.net/u013045971/article/details/46448975 1 在.c文件中,引用头文件,定义TAG.LOG宏: #include <and ...

  10. cmder 添加到右键菜单

    管理员权限打开cmde 输入: cmder /register all 回车,OK