作者: 负雪明烛
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 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的数组表示的末尾添加一个val构成一个新的数组表示,并生成最大树B,返回新最大树的root节点。

最大树就是找出数组中最大的元素作为根节点,该最大元素左边元素当做左子树、右边元素当做右子树。

解题方法

递归

本来拿到这个题,想的第一种方法是先求出A的数组表示,其求法是对于最大树,从左到右竖着看,和每个节点相交的顺序拼起来就是答案。不过这个做法我们写出来。

然后就直接用递归了,这个题最重要的一个信息就是在最大树A的数组表示的末尾添加val,也就是单词append.如果会python的应该都明白,不过也发现有的同学不懂append的含义,然后不明白题目意思了。

递归最重要的是明白递归函数的意义:insertIntoMaxTree(TreeNode* root, int val)代表了向root子树的数组表示法的末尾添加一个值为val的新节点,并把结果进行返回。

  1. 如果原来的A不存在,那么很简单要返回该新节点。
  2. 如果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++)的更多相关文章

  1. 【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 ...

  2. 【LeetCode】968. Binary Tree Cameras 解题报告(C++)

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

  3. 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)

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

  4. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

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

  5. 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)

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

  6. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  7. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  8. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  9. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

随机推荐

  1. linux安全性增加

    账户安全问题 Linux  默认会安装很多不必要的用户和用户组,如果不需要某些用户或者组,就要立即删除它,因为账户越多,系统就越不安全,很可能被黑客利用,进而威胁到服务器的安全. Linux系统中可以 ...

  2. 苹果ios通过描述文件获取udid

    苹果ios通过描述文件获取udid 需要准备的东西 1,安装描述文件只支持https的回调地址,所以需要申请https域名 2,描述文件签名,不安装也可,只要能接受红色的字 步骤: 1,准备xml文件 ...

  3. 『学了就忘』Linux文件系统管理 — 62、手动分配swap分区

    目录 1.查看swap分区情况 2.手动修改swap分区 3.格式化swap分区 4.使用swap分区 5.配置swap分区开机之后自动挂载 1.查看swap分区情况 swap分区就相当于是内存的一个 ...

  4. pymongdb入门

    Pymongo入门 安装 pip install pymongo 连接 实际就是实例化一个客户端对象,然后客户端对象中指定一个库作为库对象,库对象中的集合对象就是之后常用来执行操作的对象 1 ''' ...

  5. Go语言核心36讲(Go语言实战与应用二十三)--学习笔记

    45 | 使用os包中的API (下) 我们在上一篇文章中.从"os.File类型都实现了哪些io包中的接口"这一问题出发,介绍了一系列的相关内容.今天我们继续围绕这一知识点进行扩 ...

  6. Hive(八)【行转列、列转行】

    目录 一.行转列 相关函数 concat concat_ws collect_set collect_list 需求 需求分析 数据准备 写SQL 二.列转行 相关函数 split explode l ...

  7. nodeJs-querystring 模块

    JavaScript 标准参考教程(alpha) 草稿二:Node.js querystring 模块 GitHub TOP querystring 模块 来自<JavaScript 标准参考教 ...

  8. MyBatis 如何实现流式查询

    基本概念 流式查询指的是查询成功后不是返回一个集合而是返回一个迭代器,应用每次从迭代器取一条查询结果.流式查询的好处是能够降低内存使用. 如果没有流式查询,我们想要从数据库取 1000 万条记录而又没 ...

  9. mysql删除数据后不释放空间问题

    如果表的引擎是InnoDB,Delete From 结果后是不会腾出被删除的记录(存储)空间的. 需要执行:optimize table 表名; eg:optimize table eh_user_b ...

  10. 【Service】【Web】【Middleware】Tomcat

    1. 概念 1.1. 官方网站:tomcat.apache.org 1.2. tomcat的组件 <Server> <Service> <Connector/> & ...