【LeetCode】998. Maximum Binary Tree II 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 following Construct(A) routine:
- If
Ais empty, returnnull. - Otherwise, let
A[i]be the largest element ofA. Create a root node with valueA[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 <= B.length <= 100
题目大意
给出了一个最大树A,在最大树A的数组表示的末尾添加一个val构成一个新的数组表示,并生成最大树B,返回新最大树的root节点。
最大树就是找出数组中最大的元素作为根节点,该最大元素左边元素当做左子树、右边元素当做右子树。
解题方法
递归
本来拿到这个题,想的第一种方法是先求出A的数组表示,其求法是对于最大树,从左到右竖着看,和每个节点相交的顺序拼起来就是答案。不过这个做法我们写出来。
然后就直接用递归了,这个题最重要的一个信息就是在最大树A的数组表示的末尾添加val,也就是单词append.如果会python的应该都明白,不过也发现有的同学不懂append的含义,然后不明白题目意思了。
递归最重要的是明白递归函数的意义:insertIntoMaxTree(TreeNode* root, int val)代表了向root子树的数组表示法的末尾添加一个值为val的新节点,并把结果进行返回。
- 如果原来的A不存在,那么很简单要返回该新节点。
- 如果A存在,在A的数组表示的最后新添加了一个val,两种情况:
- 如果val > root.val,已知root.val是数组中最大的元素了,所以,此时会形成一个新的根节点,并把原来的A当做该根节点的左子树
- 如果val < root.val,那么仍然要记住该节点是在最大元素的右边,所以根节点的右子树变成了在根节点的右子树插入val的新子树。
C++代码如下:
/**
* 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* insertIntoMaxTree(TreeNode* root, int val) {
if (!root) return new TreeNode(val);
if (val > root->val) {
TreeNode* newRoot = new TreeNode(val);
newRoot->left = root;
return newRoot;
} else {
root->right = insertIntoMaxTree(root->right, val);
}
return root;
}
};
日期
2019 年 2 月 24 日 —— 周末又结束了
【LeetCode】998. Maximum Binary Tree II 解题报告(C++)的更多相关文章
- 【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 ...
- 【LeetCode】968. Binary Tree Cameras 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
随机推荐
- [R] 保存pheatmap图片对象到文件
一般我们使用pheatmap通过Rstudio交互得到的图片在plots的Export导出即可,如何保存对象到文件呢?这个需求在自动化流程中很常见,作者似乎也没说明. 生成示例数据: test = m ...
- python-django-自定义查询Q函数和F函数
数据库: def page_q(request): """Q函数的使用""" #查询username和nickname都是zhangsan ...
- Scrapy爬虫框架的安装和使用
Scrapy是一个十分强大的爬虫框架,依赖的库比较多,至少需要依赖的库有Twisted 14.0.lxml 3.4和pyOpenSSL 0.14.在不同的平台环境下,它所依赖的库也各不相同,所以在安装 ...
- Angular中怎样创建service服务来实现组件之间调用公共方法
Angular组件之间不能互相调用方法,但是可以通过创建服务来实现公共方法的调用. 实现 创建服务命令 ng g service 服务路径/服务名 比如这里在app/services目录下创建stor ...
- 快速挂起VIM以及调出被挂起的VIM的方法
vim中开了多窗口后有时需要临时切出去执行shell指令,查看结果,在vim中用%很不方便查看结果,要切出去又要逐个小窗口:q,非常麻烦. 上网一查竟然有挂起的方法: 挂起:ctrl-z 调出:fg ...
- Shell学习(三)——Shell条件控制和循环语句
参考博客: [1]Shell脚本的条件控制和循环语句 一.条件控制语句 1.if语句 1.1语法格式: if [ expression ] then Statement(s) to be execut ...
- android studio 生成aar和引用aar
以android studio 2.0正式版为例 1.aar包是Android studio下打包android工程中src.res.lib后生成的aar文件,aar包导入其他android stud ...
- Kafaka相关命令
开启zookeeper命令(备注:先进入zookeeper的bin目录) ./zkServer.sh start 关闭zookeeper命令(备注:先进入zookeeper的bin目录) ./zkSe ...
- 使用beanUtils封装对象的servlet
package com.hopetesting.web.servlet;import com.hopetesting.dao.UserDao;import com.hopetesting.domain ...
- 【力扣】922. 按奇偶排序数组 II
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述条件的数组 ...