题目如下:

We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree.

Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with the following Construct(A) routine:

  • If A is empty, return null.
  • Otherwise, let A[i] be the largest element of A.  Create a root node with value A[i].
  • The left child of root will be Construct([A[0], A[1], ..., A[i-1]])
  • The right child of root will be Construct([A[i+1], A[i+2], ..., A[A.length - 1]])
  • Return root.

Note that we were not given A directly, only a root node root = Construct(A).

Suppose B is a copy of A with the value val appended to it.  It is guaranteed that B has unique values.

Return Construct(B).

Example 1:

Input: root = [4,1,3,null,null,2], val = 5
Output: [5,4,null,1,3,null,null,2]
Explanation: A = [1,4,2,3], B = [1,4,2,3,5]

Example 2:

Input: root = [5,2,4,null,1], val = 3
Output: [5,2,4,null,1,null,3]
Explanation: A = [2,1,5,4], B = [2,1,5,4,3]

Example 3:

Input: root = [5,2,3,null,1], val = 4
Output: [5,2,4,null,1,3]
Explanation: A = [2,1,5,3], B = [2,1,5,3,4]

Note:

  1. 1 <= B.length <= 100

解题思路:本题的题意需要好好理解一番,大概意思是这样。给定树A的根节点,并且A是一个最大二叉树(最大二叉树的定义见题目中previous problem),根据特性将A还原成一个数组的表示,如用例1是[1,4,2,3],在这个数组后面追加一个元素后变成[1,4,2,3,5],再根据这个新得的数组构造出树B。那么解题方法也可以分成两部,先是还原成数组,这个用递归即可;二是构造树,见【leetcode】654. Maximum Binary Tree 。

代码如下:

# 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 decompile(self,node):
if node == None:
return []
return self.decompile(node.left) + [node.val] + self.decompile(node.right) def insertIntoMaxTree(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
path = self.decompile(root) + [val]
newRoot = TreeNode(None)
self.build(newRoot, path)
return newRoot

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

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

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

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

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

  3. 【leetcode】654. Maximum Binary Tree

    题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...

  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. java生成随机数方法

    java生成随机数方法: String random = String.valueOf(new Random().nextInt(9000)); String timestamp = String.v ...

  2. ci常量

    1. ENVIRONMENT产品的环境,有3种环境,分别是: development开发环境 testing测试环境 production生产环境 2. SELFCI的主入口文件名称 例如我的是: i ...

  3. 欢迎页jsp

    <!-- 拦截设置 --><servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> ...

  4. 一次服务器CPU占用100%的问题排查

    今天写了一段垃圾代码,然后上服务器上运行,cpu瞬间飙到了100%,现记录一下问题排除过程~ 1. 问题代码 package qinfeng.zheng.mockmvcdemo; import org ...

  5. 【Linux】设置开机自启

    忘记转发的哪里的. 方法 1这种方法会利用 /etc/ 中的 rc.local 文件来在启动时执行脚本与命令.我们在文件中加上一行来执行脚本,这样每次启动系统时,都会执行该脚本. 不过我们首先需要为 ...

  6. [CSP-S模拟测试]:antipalindrome(数学)

    题目传送门(内部题58) 输入格式 第一行一个数$T$表示数据组数.接下来每行两个数$n$和$m$. 输出格式 $T$行,每行一个答案,对${10}^9+7$取模. 样例 样例输入: 25 66 5 ...

  7. response.setHeader();小结

    response.setHeader():1. HTTP消息头 (1)通用信息头 即能用于请求消息中,也能用于响应信息中,但与被传输的实体内容没有关系的信息头,如Data,Pragma 主要: Cac ...

  8. mysql null 值查询问题

    我在开发公司内部的一个项目时遇到一个问题:select student_quality_id from STUDENT_QUALITY where mark_status=0 and batch_st ...

  9. FWT公式一览

    总表 真值表 对应运算 FWT IFWT A=B=0 A≠B A=B=1 左项 右项 左项 右项 0 0 1 & L+R R L-R R 0 1 0 ^ L+R L-R (L+R)/2 (L- ...

  10. C#后台将string="23.00"转换成int类型

    在C# 后台将String类型转换成int 有以下几种方法: (1)  int.TryParse(string); (2) Convert.Toint32(string); (3) (int)stri ...