题目如下:

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

  1. The root is the maximum number in the array.
  2. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  3. 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:

  1. The size of the given array will be in the range [1,1000].

解题思路:题目不难,从72%通过率就能看出来。方法是找出数组的最大值并作为根节点,然后把数组以最大值为界分成左右两部分;之后再对左右两部分分别递归,直到数组被分割完成为止。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def build(self,node,nums):
v = max(nums)
inx = nums.index(v)
node.val = v ll = nums[:inx]
rl = nums[inx+1:]
if len(ll) > 0:
left = TreeNode(None)
node.left = left
self.build(left,ll)
if len(rl) > 0:
right = TreeNode(None)
node.right = right
self.build(right,rl) def constructMaximumBinaryTree(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
root = TreeNode(None)
self.build(root,nums)
return root

【leetcode】654. Maximum Binary Tree的更多相关文章

  1. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

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

  3. 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  4. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  5. 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...

  6. 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...

  7. 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  8. 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...

  9. 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

随机推荐

  1. 一款易用、高可定制的vue翻页组件

    一款易用.高可定制的vue翻页组件 在线体验:pages.cixi518.com 使用 npm i vo-pages --save vo-pages组件父元素必须设置固定高度并填写属性overflow ...

  2. SpringBoot JSON文件读取

    @Componentpublic class StepExecutor implements Runnable { @Value("classpath:menu.json") pr ...

  3. Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes

    问题描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  4. shell脚本学习(4)cut

    cut 的两种用法 1种是 -c list   剪切字符串中特定位置的文字, /etc/passwd中的原始数据: yuyuyu:x:1000:1000:yuyuyu,,,:/home/yuyuyu: ...

  5. http 换成 https

    UPDATE SYS_MENU M SET M.href = ( SELECT CASE WHEN substr(N.href, 0, 5) = 'http:' THEN 'https:'||subs ...

  6. Sublime Text 3 快捷键总结(Mac)

    Command + Shift + L 光标同时定位多行 Command + Enter 在下一行插入新行.举个栗子:即使光标不在行尾,也能快速向下插入一行.

  7. Xcode 10如何打包ipa包?

    参考: https://www.jianshu.com/p/0421b3fd2470 前置条件 首先导入证书和配置文件 具体操作步骤: product>>Archive 如图所示,选择Di ...

  8. Tecplot 360 安装后弹出“Is your Tecplot 360 EX liense valid?”解决方法

    在hosts文件中添加127.0.0.1 download.tecplot.com这句指令时,应注意1与download之间有空格!

  9. 在线清空nohup.out内容

    通过 cat /dev/null > filename 命令可以在线清空nohup.out里的内容

  10. CPU的架构:x86、arm、mips、龙芯等

    在公司经常听其他工程师讲x86,arm平台啥的,作为一个算法工程师,我听过却不知道这是啥!!!(汗颜) 现在偷偷学起: x86,arm,mips等这些都是CPU的架构,不管是手机电脑还是一些嵌入式的设 ...