题目地址:https://leetcode.com/problems/maximum-binary-tree/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 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:

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

题目大意

根据数组的最大值分割进行构建二叉树。

解题方法

递归

找到数组中的最大值和最大值的位置,然后用切片的方式进行构建。如果数组为空,那么叶子为空。

Python代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def constructMaximumBinaryTree(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums:
return None
_max = max(nums)
max_pos = nums.index(_max)
root = TreeNode(_max)
root.left = self.constructMaximumBinaryTree(nums[:max_pos])
root.right = self.constructMaximumBinaryTree(nums[max_pos + 1:])
return root

C++代码如下:

/**
* 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) {
int N = nums.size();
if (N == 0) return nullptr;
int ind = 0, curMax = INT_MIN;
for (int i = 0; i < N; i++) {
if (nums[i] > curMax) {
curMax = nums[i];
ind = i;
}
}
TreeNode* node = new TreeNode(curMax);
vector<int> leftv = vector<int>(nums.begin(), nums.begin() + ind);
vector<int> rightv = vector<int>(nums.begin() + ind + 1, nums.end());
node->left = constructMaximumBinaryTree(leftv);
node->right = constructMaximumBinaryTree(rightv);
return node;
}
};

日期

2018 年 2 月 5 日
2018 年 12 月 2 日 —— 又到了周日
2019 年 9 月 27 日 —— 昨天面快手,竟然是纯刷题

最新文章

  1. .Net中的AOP系列之构建一个汽车租赁应用
  2. javascript-with()方法
  3. struts2笔记4
  4. object-c NSString 转成特定编码格式如utf8、gbk等
  5. 靓号正则表达式(前后向查找等) 和 apache正则包使用
  6. Delphi 版本号(D1到XE6),发现一个delphi.wikia.com网站
  7. Ubuntu 12.04下安装ibus中文输入法
  8. 谁的用户在世界上是&amp;#160;&amp;#160;明基决心保时捷设计标准
  9. SICP 锻炼 (1.45)解决摘要
  10. 通过logstash-input-mongodb插件将mongodb数据导入ElasticSearch
  11. 内存数据网格IMDG简介
  12. C语言之输出空心棱形图案
  13. 【Jquery+Express.js】 submit() 方法提交form
  14. Truncated Power Method for Sparse Eigenvalue Problems
  15. Mac下更改JDK环境变量配置
  16. XtraBackup的备份原理与应用示例
  17. 20155332 2016-2017-2 《Java程序设计》第9周学习总结
  18. python函数进阶
  19. n元一维向量向左循环移位i的几种算法
  20. 不一样的入门:看C# Hello World的17种写法

热门文章

  1. linux—查看所有的账号以及管理账号
  2. 【学相伴】Nginx最新教程通俗易懂-狂神说
  3. 记一次 .NET 某化妆品 webapi 卡死分析
  4. Spring Boot 热启动插件
  5. Scala(二)【基本使用】
  6. 大数据学习day32-----spark12-----1. sparkstreaming(1.1简介,1.2 sparkstreaming入门程序(统计单词个数,updateStageByKey的用法,1.3 SparkStreaming整合Kafka,1.4 SparkStreaming获取KafkaRDD的偏移量,并将偏移量写入kafka中)
  7. 零基础学习java------33---------http协议,tomcat(其如何在eclipse上发布),注册案例
  8. k8s-hpa自动横向扩容
  9. 一起手写吧!sleep函数!
  10. Linux基础命令----smbclient