【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 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
A
is empty, returnnull
.- Otherwise, let
A[i]
be the largest element ofA
. Create aroot
node with valueA[i]
.- The left child of
root
will beConstruct([A[0], A[1], ..., A[i-1]])
- The right child of
root
will 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
B
is a copy ofA
with the valueval
appended to it. It is guaranteed thatB
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 <= 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. ...
随机推荐
- NetCore中的环境变量的值取自于哪里?
环境 操作系统 win10 IIS 10 net core 2.2 ,net core 3.0 分别生成了三个环境变量的配置文件: 以及测试代码: public void Configure(IApp ...
- 英语单词operand
operand 来源 [root@centos7 ~]# mkdir mkdir: missing operand 缺少操作数,也就是要创建的目录 Try 'mkdir --help' for mo ...
- session应用:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" ...
- iview input实现默认获取焦点并选中文字
1. 业务背景 配置页面,可新建和复制任务:当复制任务的时候,要把名字的input框默认获取焦点,并全选任务名.效果如下: 2. 代码实现 <template> <Form :mod ...
- QUartus 使用之001_空格_箭头切换----FPGA--001
Quartus里的Tab键,按下后,显示的为什么是箭头,以前显示的是空白,图片如下: 解决方式如下: 修改后效果图如下:
- LUOGU P3759 [TJOI2017]不勤劳的图书管理员(树套树)
传送门 解题思路 和以前做过的一道题有点像,就是区间逆序对之类的问题,用的是\(BIT\)套权值线段树,交换时讨论一下计算答案..跑的不如暴力快.. 代码 #include<iostream&g ...
- soj#532 set p3175
传送门 分析 代码 #include<bits/stdc++.h> using namespace std; ; <<],Ans; int n,m,N; inline int ...
- 测开之路四十九:用Django实现扑克牌游戏
用Django实现和之前flask一样的扑克牌游戏 项目结构 html <!DOCTYPE html><html lang="en"><head> ...
- java常用排序
1.冒泡排序 public static int[] bubble(int[] a){ for(int i=0;i<a.length-1;i++){ int tmp=0; for(int j=0 ...
- Asp.Net Core 第07局:路由
总目录 前言 本文介绍Asp.Net Core 路由. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:路由概述 1.路由主要用于处理特定的请求. ...