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. Rolling Hash about the Rsync

    今天看文献看到一个有趣的算法—Rolling Hash,这个算法可以更新在不同的machine上的两个“similar”的文件,也叫做rsync algorithm,rsync顾名思义:remote ...

  2. python之斐波那契数列递归推导在性能方面的反思

    在各种语言中,谈到递归首当其冲的是斐波那契数列,太典型了,简直就是标杆 一开始本人在学习递归也是如此,因为太符合逻辑了 后台在工作和学习中,不断反思递归真的就好嘛? 首先递归需要从后往前推导,所有数据 ...

  3. html和body的关系

    1.chrome浏览器 从上面数据我们可以看出,html作为body的容器,其大小就是我们可视窗口的大小,window.innerHeight与html的clientHeight相同即可验证. 同时, ...

  4. DeepLab 使用 Cityscapes 数据集训练模型

    原文地址:DeepLab 使用 Cityscapes 数据集训练模型 0x00 操作环境 OS: Ubuntu 16.04 LTS CPU: Intel® Core™ i7-4790K GPU: Ge ...

  5. 从零开始学 Web 之 移动Web(八)Less

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  6. 通过GitHub和GoDaddy搭建静态个人博客

    一.创建GitHub个人主页 通过GitHub创建个人博客的好处:无广告,因为页面代码全是你自己写的:免费,不用买主机:装X:设计和编码练习. 1. 在GitHub上创建一个新的repository, ...

  7. leetcode — 4sum

    import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...

  8. JS判断是否是数组的四种做法

    一.前言 如何判断一个对象或一个值是否是一个数组,在面试或工作中我们常常会遇到这个问题,既然出现频率高,想着还是做个整理,那么本文主要基于几种判断方式,以及方式判断的原理,是否存在问题展开讨论. 二. ...

  9. Angularjs 通过asp.net web api认证登录

    Angularjs 通过asp.net web api认证登录 Angularjs利用asp.net mvc提供的asp.net identity,membership实现居于数据库的用户名/密码的认 ...

  10. 【学习笔记】JS设计模式总结

    前言:这段时间都在学习Vue的知识,虽然手边放着一本js高程,但确实好久没有好好复习了.温故而知新,因此特意把JS常见的设计模式总结,希望对大家有所帮助... 1. 工厂模式 释义:像工厂一样流水线般 ...