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. OS-96

    print('os.access(path,mode):检验权限模式----------------------------------------------------------------') ...

  2. [LeetCode] 717. 1-bit and 2-bit Characters_Easy

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  3. [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  4. pandas取dataframe特定行/列

    1. 按列取.按索引/行取.按特定行列取 import numpy as np from pandas import DataFrame import pandas as pd df=DataFram ...

  5. Qt addStretch()详解

    addStretch函数,是在布局的时候用到. 函数原型: void QBoxLayout::addStretch ( int stretch = 0 ) 作用:平均分配Layout 比如: QVBo ...

  6. <转>ORACLE EBS中查看某个Request的Output File

    由于某些权限的限制,有时候哪怕System Administrator职责也只能看到某个Request信息,但是不能查看它的Output File(在“Requests Summary”窗口中“Vie ...

  7. cc150 --链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.   快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个.     public class Solution { public Li ...

  8. Atcoder Tenka1 Programmer Contest 2019 D Three Colors

    题意: 有\(n\)个石头,每个石头有权值,可以给它们染'R', 'G', 'B'三种颜色,如下定义一种染色方案为合法方案: 所有石头都染上了一种颜色 令\(R, G, B\)为染了'R', 染了'G ...

  9. Spring整合ActiveMQ:spring+JMS+ActiveMQ+Tomcat

    一.目录结构 相关jar包 二.关键配置activmq.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  10. Python-sys模块,异常

    习题1:题目:给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. #encoding=utf-8 while True: try: num=int(raw_input(&quo ...