LeetCode题解Maximum Binary Tree
1、题目描述
2、分析
找出最大元素,然后分割数组调用。
3、代码
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
int size = nums.size();
if (size == )
return NULL;
TreeNode *dummy = new TreeNode();
maxcont(dummy->left, , size-, nums);
return dummy->left;
} void maxcont(TreeNode* &parent, int left, int right, vector<int>& nums)
{
if (left > right )
return ;
int maxindex = find_max(left, right, nums);
TreeNode *tmp = new TreeNode(nums[maxindex]);
parent = tmp;
maxcont(tmp->left, left, maxindex-, nums);
maxcont(tmp->right, maxindex + , right, nums);
} int find_max(int left, int right, vector<int> &nums)
{
int n = ;
int maxVal = INT_MIN;
for (int i = left; i <= right ; i++) {
if ( nums[i] > maxVal) {
maxVal = nums[i];
n = i;
} }
return n;
}
LeetCode题解Maximum Binary Tree的更多相关文章
- [Leetcode Week14]Maximum Binary Tree
Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...
- LeetCode - 654. Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- LeetCode 654. Maximum Binary Tree最大二叉树 (C++)
题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...
- leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- [LeetCode 题解]: Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- [LeetCode] 654. Maximum Binary Tree 最大二叉树
Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...
- LeetCode题解之Binary Tree Right Side View
1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if ...
- LeetCode题解之Binary Tree Pruning
1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...
随机推荐
- redis epoll 原理梗概
redis 是一个单线程却性能非常好的内存数据库, 主要用来作为缓存系统. redis 采用网络IO多路复用技术来保证在多连接的时候, 系统的高吞吐量.为什么 Redis 中要使用 I/O 多路复用这 ...
- [EXP]Adobe ColdFusion 2018 - Arbitrary File Upload
# Exploit Title: Unrestricted # Google Dork: ext:cfm # Date: -- # Exploit Author: Pete Freitag of Fo ...
- Linux学习笔记之四————Linux常用命令之文件管理
Linux命令——文件管理相关命令 <1>查看文件信息:ls ls是英文单词list的简写,其功能为列出目录的内容,是用户最常用的命令之一,它类似于DOS下的dir命令. Linux文件或 ...
- 使用docker-compose快速构建集群示例(一)
一.zookeeper集群 docker-compose文件: version: '3.1' services: zoo1: image: zookeeper hostname: zoo1 conta ...
- 使用Windows的mstsc远程桌面连接到Ubuntu图形界面(AWS上安装的Ubuntu系统)
参考文档:https://blog.csdn.net/liumaolincycle/article/details/50052619 https://www.cnblogs.com/eczhou/p/ ...
- .NET 线程池编程技术
摘要 深度探索 Microsoft .NET提供的线程池, 揭示什么情况下你需要用线程池以及 .NET框架下的线程池是如何实现的,并告诉你如何去使用线程池. 内容 介绍 .NET中的线程池 线程池中执 ...
- 交换路由中期测验20181226(动态路由配置与重分发、NAT转换、ACL访问控制列表)
测试拓扑: 接口配置信息 HostName 接口 IP地址 网关 Server 0 Fa0 172.16.15.1/24 172.16.15.254 Server 1 Fa0 100.2.15.200 ...
- Python机器学习笔记:利用Keras进行分类预测
Keras是一个用于深度学习的Python库,它包含高效的数值库Theano和TensorFlow. 本文的目的是学习如何从csv中加载数据并使其可供Keras使用,如何用神经网络建立多类分类的数据进 ...
- python的Web框架,html分页
使用简单的算法得出页码数,然后在html中获取即可.仅供参考. views的写法 def crm_stu(request): section = '教师后台管理页' search = request. ...
- Spark SQL结构化数据处理
Spark SQL是Spark框架的重要组成部分, 主要用于结构化数据处理和对Spark数据执行类SQL的查询. DataFrame是一个分布式的,按照命名列的形式组织的数据集合. 一张SQL数据表可 ...