【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- ...
随机推荐
- 【Python小试】计算蛋白序列中指定氨基酸所占的比例
编码 from __future__ import division def get_aa_percentage(protein, aa_list=['A','I','L','M','F','W',' ...
- zabbix 集成cloud alert
1. 了解 Cloud Alert 通过应用,接入监控系统/平台的告警,集中管理您的告警,统一分派通知,统一分析.这个平台最先了解和使用是在 2017 年下半年,之前的名称叫 oneits ...
- C语言 fastq文件转换为fasta文件2
修改可读取压缩格式文件 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #in ...
- excel-大于0的数值标记红色且标记红色上箭头,小于0的数值标记绿色且标记绿色下箭头,等于0的数值标记黄色且标记右箭头
0.数值是常规的数值: [蓝色]"↑"0;[红色]"↓"0;[黄色]"→"0 [蓝色]"↑"0.0;[红色]" ...
- Identity Server 4 从入门到落地(五)—— 使用Ajax访问Web Api
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- 学习 DDD - 通用语言的模式
大家好,我是霸戈,这周学习了一些关于领域驱动设计的知识 ,对比较深刻的地方做了不少笔记,分享给大家. 在日常需求讨论的时候,经常会碰到一个需求会议开了一个多小时还没有达成共识.作为业务方(领域专家)明 ...
- Android editttext只能输入不能删除(选中后被软键盘遮住)
感谢https://www.dutycode.com/post-20.html: 解决方法:在布局外外嵌一层scrollview.
- day03 Django目录结构与reques对象方法
day03 Django目录结构与reques对象方法 今日内容概要 django主要目录结构 创建app注意事项(重点) djago小白必会三板斧 静态文件配置(登录功能) requeste对象方法 ...
- 数仓day02
1. 什么是ETL,ETL都是怎么实现的? ETL中文全称为:抽取.转换.加载 extract transform load ETL是传数仓开发中的一个重要环节.它指的是,ETL负责将分布的. ...
- 一起手写吧!call、apply、bind!
apply,call,bind都是js给函数内置的一些api,调用他们可以为函数指定this的执行,同时也可以传参. call call 接收多个参数,第一个为函数上下文也就是this,后边参数为函数 ...