LeetCode——Maximum Binary Tree
Question
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
The root is the maximum number in the array.
The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
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].
Solution
递归求解。
Code
/**
* 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) {
struct TreeNode* root;
return construct(root, nums, 0, nums.size() - 1);
}
TreeNode* construct(struct TreeNode* root, vector<int>& nums, int start, int end) {
if (start > end) {
return NULL;
}
int max_value = INT_MIN;
int max_index = start;
for (int i = start; i <= end; i++) {
if (nums[i] > max_value) {
max_value = nums[i];
max_index = i;
}
}
root = new TreeNode(max_value);
root->left = construct(root, nums, start, max_index - 1);
root->right = construct(root, nums, max_index + 1, end);
return root;
}
};
LeetCode——Maximum Binary Tree的更多相关文章
- [LeetCode] Maximum Binary Tree 最大二叉树
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- [Leetcode Week14]Maximum Binary Tree
Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...
- Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)
Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...
- 【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 ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- leetcode_998. Maximum Binary Tree II
https://leetcode.com/problems/maximum-binary-tree-ii/ 在654. Maximum Binary Tree版本的建树基础上,在最后插入一个数. 新节 ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
随机推荐
- 万恶之源 - Python基础
Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程 ...
- Python一键安装全部依赖包
requirements.txt用来记录项目所有的依赖包和版本号,只需要一个简单的pip命令就能完成. pip freeze >requirements.txt 然后就可以用 pip insta ...
- libsvm使用
先挖个坑,有空重写svm_scale, svm_train, svm_predict几个代码,给的实在写的不敢恭维 package org.ml.svm; import java.io.File; i ...
- python 皮尔森相关系数
皮尔森理解 皮尔森相关系数(Pearson correlation coefficient)也称皮尔森积矩相关系数(Pearson product-moment correlation coeffic ...
- linux中的各种$号 位置参数变量
位置参数变量 $n #/bin/bash echo $0(代表命令本身); echo $1; (代表第几个参数) echo $2; [root@LocalWeb01 ~]# ./1.sh ...
- CE寻找游戏基址
什么是游戏基址? 游戏基址是保持恒定的两部分内存地址的一部分并提供一个基准点,从这里可以计算一个字节数据的位置.基址伴随着一个加到基上的偏移值来确定信息准确的位置(绝对地址). 全局基址 一级基址 二 ...
- 【week6】团队贡献分
小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app 完成任务: 10% 20% 70% 好 于淼 李权 中 刘芳芳 杨柳 差 1.李权8.4 2.于 ...
- 隐马尔科夫模型(HMM)学习笔记二
这里接着学习笔记一中的问题2,说实话问题2中的Baum-Welch算法编程时矩阵转换有点烧脑,开始编写一直不对(编程还不熟练hh),后面在纸上仔细推了一遍,由特例慢慢改写才运行成功,所以代码里面好多处 ...
- 使用Jmeter测试Dubbo接口(参数设置篇)
WebSocket接口需要下载dubbo插件才能使用 本次下载的版本为jmeter-plugins-dubbo-1.3.6,下载完成后jar文件放到\lib\ext目录下 由于工作需要,最近需要对du ...
- Azkaban 入门
需求 实际当中经常有这些场景:每天有一个大任务,这个大任务可以分成A,B,C,D四个小任务,A,B任务之间没有依赖关系,C任务依赖A,B任务的结 果,D任务依赖C任务的结果.一般的做法是,开两个终端同 ...