【leetcode】998. Maximum Binary Tree II
题目如下:
We are given the
rootnode 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 followingConstruct(A)routine:
- If
Ais empty, returnnull.- Otherwise, let
A[i]be the largest element ofA. Create arootnode with valueA[i].- The left child of
rootwill beConstruct([A[0], A[1], ..., A[i-1]])- The right child of
rootwill beConstruct([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
Bis a copy ofAwith the valuevalappended to it. It is guaranteed thatBhas 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 <= 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的更多相关文章
- 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【leetcode】654. Maximum Binary Tree
题目如下: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...
- 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...
- 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
随机推荐
- idea中创建.xml文件或别的文件
- iOS项目开发中的知识点与问题收集整理①
前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...
- 前端-使用template-web.js【模版引擎】渲染数据
通过ajax请求到的数据,使用传统的拼接字符串也可以做到,但是‘ “ ” ‘这种模式,单引号.双引号容易 扩错,新手入门推荐使用这种,初入前端,需要一个一个敲代码体会一下. 使用 template.j ...
- Idea的几个常用的
sout+tab= "System.out.println()" ctrl+alt+v=生成当前对象的实例 ctrl+shift+enter="(真个是真的牛哦)直接 ...
- 如何隐藏一个让人很难发现的bug?
程序员的日常三件事:写bug.改bug.背锅.连程序员都自我调侃道,为什么每天都在加班?因为我的眼里常含bug. 那么如何写出一个让(坑)人(王)很(之)难(王)发现的bug呢? - 1 - 新手开发 ...
- ueditor 图片粘贴上传,实现图文粘贴,图片自动上传
如何做到 ueditor批量上传word图片? 1.前端引用代码 <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN& ...
- Python基础教程(019)--执行Python的方式,IPython
前言 了解IPython 内容 IPython 是一个Python的交互式shell,比默认的Python shell 好用的多 查看图片 在提示符下执行 目的 了解进入IPython 退出IPyth ...
- NOIp 图论算法专题总结 (2)
系列索引: NOIp 图论算法专题总结 (1) NOIp 图论算法专题总结 (2) NOIp 图论算法专题总结 (3) 树链剖分 https://oi-wiki.org/graph/heavy-lig ...
- [CSP-S模拟测试]:gcd(莫比乌斯反演)
题目描述 有$n$个正整数$x_1\sim x_n$,初始时状态均为未选.有$m$个操作,每个操作给定一个编号$i$,将$x_i$的选取状态取反.每次操作后,你需要求出选取的数中有多少个互质的无序数对 ...
- (转)VirtualBox下安装CentOS7系统
转:https://www.cnblogs.com/hihtml5/p/8217062.html 本文假定你已经知道如何安装VirtualBox虚拟机软件,并且已经安装好了. 首先我们需要准备好cen ...





