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].

这道题给了我们一个数组,让我们创建一个最大二叉树,创建规则是数组中的最大值为根结点,然后分隔出的左右部分再分别创建最大二叉树。那么明眼人一看就知道这是分治法啊,果断上递归啊。首先就是要先找出数组中的最大值,由于数组是无序的,所以没啥好的办法,就直接遍历吧,找到了最大值,就创建一个结点,然后将左右两个子数组提取出来,分别调用递归函数并将结果连到该结点上,最后将结点返回即可,参见代码如下:

解法一:

class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
if (nums.empty()) return NULL;
int mx = INT_MIN, mx_idx = ;
for (int i = ; i < nums.size(); ++i) {
if (mx < nums[i]) {
mx = nums[i];
mx_idx = i;
}
}
TreeNode *node = new TreeNode(mx);
vector<int> leftArr = vector<int>(nums.begin(), nums.begin() + mx_idx);
vector<int> rightArr = vector<int>(nums.begin() + mx_idx + , nums.end());
node->left = constructMaximumBinaryTree(leftArr);
node->right = constructMaximumBinaryTree(rightArr);
return node;
}
};

下面这种方法也是递归的解法,和上面的解法稍有不同的是不必提取子数组,而是用两个变量来指定子数组的范围,其他部分均和上面的解法相同,参见代码如下:

解法二:

class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
if (nums.empty()) return NULL;
return helper(nums, , nums.size() - );
}
TreeNode* helper(vector<int>& nums, int left, int right) {
if (left > right) return NULL;
int mid = left;
for (int i = left + ; i <= right; ++i) {
if (nums[i] > nums[mid]) {
mid = i;
}
}
TreeNode *node = new TreeNode(nums[mid]);
node->left = helper(nums, left, mid - );
node->right = helper(nums, mid + , right);
return node;
}
};

下面这种解法是论坛上的高分解法,使用到了一个辅助数组v来让保持降序。我们遍历数组,对于每个遍历到的数字,创建一个结点,然后进行循环,如果数组v不空,且末尾结点值小于当前数字,那么将末尾结点连到当前结点的左子结点,并且移除数组中的末尾结点,这样可以保证子结点都会小于父结点。循环结束后,如果此时数组v仍不为空,说明结点值很大,那么将当前结点连到数组末尾结点的右子结点上。之后别忘了将当前结点加入数组v中,最后返回数组v的首结点即可,如果不太容易理解的话,就把题目中的例子带入一步一步运行看一下吧,参见代码如下:

解法三:

class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
vector<TreeNode*> v;
for (int num : nums) {
TreeNode *cur = new TreeNode(num);
while (!v.empty() && v.back()->val < num) {
cur->left = v.back();
v.pop_back();
}
if (!v.empty()) {
v.back()->right = cur;
}
v.push_back(cur);
}
return v.front();
}
};

参考资料:

https://discuss.leetcode.com/topic/98457/java-c-simple-recursive-method

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 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] Invert Binary Tree 翻转二叉树

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...

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

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

  4. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

  6. 654. Maximum Binary Tree最大二叉树

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

  7. LeetCode——Maximum Binary Tree

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

  8. [leetcode]257. Binary Tree Paths二叉树路径

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  9. LeetCode Invert Binary Tree 反转二叉树

    思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...

随机推荐

  1. java中最常用jar包的用途说明

    java中最常用jar包的用途说明,适合初学者 jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实 ...

  2. java 中的IO

    什么是文件文件可认为是相关记录或放在一起的数据集合 通过流来读写文件流是指一连串流动的字符,是以先进先出方式发送信息的通道输入输出流是相对计算机的内存来说的 字节流是八位通用字节流,字符流是16位Un ...

  3. New UWP Community Toolkit - RadialGauge

    概述 New UWP Community Toolkit  V2.2.0 的版本发布日志中提到了 RadialGauge 的调整,本篇我们结合代码详细讲解  RadialGauge 的实现. Radi ...

  4. 系统右键添加cmd命令

    我们运行cmd.exe时,会发现刚刚打开时,一般提示在默认路径下: 有时候我们希望直接能够切换到某个路径下运行程序或者做某些工作,那么切换路径可能就会比较麻烦.下面我们介绍一种比较实用的方法,用鼠标右 ...

  5. Java HashMap工作原理及实现

    Java HashMap工作原理及实现 2016/03/20 | 分类: 基础技术 | 0 条评论 | 标签: HASHMAP 分享到:3 原文出处: Yikun 1. 概述 从本文你可以学习到: 什 ...

  6. XFTP连接主机文件名显示中文乱码且不能下载的解决方法

    Xftp连接主机文件名显示中文乱码且不能下载的本地解决方法 原因:Xftp编码格式问题 解决方法:把Xftp的编码格式增加UTF-8 具体步骤:打开Xftp,文件-属性,在打开的属性界面中打开&quo ...

  7. 第一次PTA作业

    题目6-1拆分实数整数及小数部分 1设计思路 (1) 第一步:阅读题目要求及所给部分. 第二步:根据题意补全相应函数. (2)流程图 无 2.实验代码 #include <stdio.h> ...

  8. js window

    window对象: browser object mode :bom对象. bom提供了独立于内容而与浏览器窗口进行交互的对象. bom主要用于管理窗口与窗口之间的通讯,因此其核心对象是window ...

  9. SpringMVC之数据传递一

    之前的博客中也说了,mvc中数据传递是最主要的一部分,从url到Controller.从view到Controller.Controller到view以及Controller之间的数据传递.今天主要学 ...

  10. Spring Security 入门(1-3-3)Spring Security - logout 退出登录

    要实现退出登录的功能我们需要在 http 元素下定义 logout 元素,这样 Spring Security 将自动为我们添加用于处理退出登录的过滤器 LogoutFilter 到 FilterCh ...