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的更多相关文章

  1. [Leetcode Week14]Maximum Binary Tree

    Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...

  2. LeetCode - 654. Maximum Binary Tree

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

  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 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  5. [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 ...

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

  7. [LeetCode] 654. Maximum Binary Tree 最大二叉树

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

  8. LeetCode题解之Binary Tree Right Side View

    1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if ...

  9. LeetCode题解之Binary Tree Pruning

    1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...

随机推荐

  1. Ubuntu 18.0.4安装docker

    第一步:如果之前安装过docker,执行下面命令删除 apt-get remove docker docker-engine docker.io 删除后执行sudo apt-get update更新软 ...

  2. Spark基础脚本入门实践2:基础开发

    1.最基本的Map用法 val data = Array(1, 2, 3, 4, 5)val distData = sc.parallelize(data)val result = distData. ...

  3. c#构造初使化的顺序

    这个很基础的知识,但我至今才意识到它.想想也很失败. 直接上代码:很简单 public class Base { ; public Base() { System.Console.WriteLine( ...

  4. iOS开发工具

    Xcode插件 几乎所有开发者都知道Alcatraz是一个开源的包管理工具,可以让我们更轻松地管理各种插件.接下来就介绍下我的最推荐的10个插件: 15.FuzzyAutocompletePlugin ...

  5. 强制清除 gradle 依赖缓存

    今天同事误上传一个库,然后又删除了... 我刚好把他上传的库给down下来了...然后项目一直报错,clean...重新编译...删build文件....全都不管用===== 好几个人研究了好久,只能 ...

  6. Java并发编程笔记之ConcurrentHashMap原理探究

    在多线程环境下,使用HashMap进行put操作时存在丢失数据的情况,为了避免这种bug的隐患,强烈建议使用ConcurrentHashMap代替HashMap. HashTable是一个线程安全的类 ...

  7. 三、TortoiseGit之配置密钥

    TortoiseGit使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥. 也就是说使用 ssh-keygen  -t rsa  -C "username@email.co ...

  8. vscode使用汇总——常用插件、常用配置、常用快捷键

    一.代码提示快捷键设置:(keybindings.json) [ { "key": "ctrl+j", "command": "- ...

  9. css布局------块元素水平垂直居中的四种方法

    HTML <div class="parent answer-1"> <div></div></div> CSS .parent { ...

  10. python字典操作和内置方法

    一 字典基本介绍 python中只有字典是映射结构,通过key取值,并且key是不可变数据类型,而value可以是任意数据类型. 字典通过一个花括号,里面存放key:value的数据结构来定义.理论上 ...