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

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

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

  2. [Leetcode Week14]Maximum Binary Tree

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

  3. Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)

    Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...

  4. 【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 ...

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

  6. leetcode_998. Maximum Binary Tree II

    https://leetcode.com/problems/maximum-binary-tree-ii/ 在654. Maximum Binary Tree版本的建树基础上,在最后插入一个数. 新节 ...

  7. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

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

  9. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

随机推荐

  1. React-生命周期的相关介绍

    1.mounting/组件插入相关 (1)componentWillMount  模板插入前 (2)render 模板插入 (3)componentDidMount 模板插入后 2.Updating/ ...

  2. Java - Spring AOP 拦截器的基本实现

    一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...

  3. 应用笔画宽度变换(SWT)来检测自然场景中的文本

    Introduction: 应用背景:是盲人辅助系统,城市环境中的机器导航等计算机视觉系统应用的重要一步.获取文本能够为许多视觉任务提供上下文的线索,并且,图像检索算法的性能很大部分都依赖于对应的文本 ...

  4. logstash的各个场景应用(配置文件均已实践过)

    场景: 1) datasource->logstash->elasticsearch->kibana 2) datasource->filebeat->logstash- ...

  5. linux 使用文件作为交换分区

    sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile ...

  6. Java-使用IO流对大文件进行分割和分割后的合并

    有的时候我们想要操作的文件很大,比如:我们想要上传一个大文件,但是收到上传文件大小的限制,无法上传,这是我们可以将一个大的文件分割成若干个小文件进行操作,然后再把小文件还原成源文件.分割后的每个小文件 ...

  7. Vue.Js添加自定义插件

    基于上篇我们讲了 在window下搭建Vue.Js开发环境 我们可以开发自己的vue.js插件发布到npm上,供大家下载使用. 1.首先打开cmd命令窗口,进入我们的工作目录下 执行 cd E:\vu ...

  8. Object之总结(一)

    一.Object类中一共有12个方法.一个私有方法,两个保护方法,9个公共方法.另外还有一个静态代码块. 1.registerNatives方法.私有静态本地无参数无返回值. 2.finalize方法 ...

  9. 018-DNS解析过程与配置DNS服务

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON ZoomImageFactor

    zw版[转发·台湾nvp系列Delphi例程]HALCON ZoomImageFactor procedure TForm1.Button1Click(Sender: TObject);var ima ...