题目地址:https://leetcode-cn.com/problems/find-leaves-of-binary-tree/

题目描述

Given a binary tree, collect a tree’s nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.

Example:

Input: [1,2,3,4,5]

          1
/ \
2 3
/ \
4 5 Output: [[4,5,3],[2],[1]] Explanation: 1. Removing the leaves [4,5,3] would result in this tree: 1
/
2 2. Now removing the leaf [2] would result in this tree: 1 3. Now removing the leaf [1] would result in the empty tree: []

题目大意

给你一棵完全二叉树,请按以下要求的顺序收集它的全部节点:

  • 依次从左到右,每次收集并删除所有的叶子节点
  • 重复如上过程直到整棵树为空

解题方法

DFS

我的做法比较新颖:计算每个节点的高度,依次放入高度为0,1,2,…,depth(root)的所有节点。

为什么?因为题目虽然让我们每次放入的都是叶子节点,而叶子节点的高度是0。当删除叶子节点时,会使剩余的每个节点的高度减一,此时新的叶子节点就是如果不删除老叶子节点时高度为1的节点……按照这个方法去做,就是依次放入高度为0,1,2,…,depth(root)的所有节点。

求树的高度用到了记忆化搜索,即代码中的node2depth,这是为了保存已经计算过高度的叶子节点,从而加速求树的高度的计算。

保存每个高度对应了哪些叶子节点的值,使用的是倒排表depth2node,其key是高度,value是该高度下对应的叶子节点的值。

DFS时遍历的方式选用的后序遍历,因为按照题目的要求,必须从左到右依次放入叶子节点,故遍历方式是左孩子->右孩子->根节点

这个做法的好处是不用修改树的结构,比如做删除叶子节点的操作。

时间复杂度是O(N),N为节点数。

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:
vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> res;
int height = depth(root);
for (int i = 0; i <= height; ++i) {
res.push_back(depth2node[i]);
}
return res;
}
int depth(TreeNode* root) {
if (!root) return -1;
if (node2depth.count(root))
return node2depth[root];
int left = depth(root->left);
int right = depth(root->right);
int cur = max(left, right) + 1;
depth2node[cur].push_back(root->val);
node2depth[root] = cur;
return cur;
}
private:
unordered_map<int, vector<int>> depth2node;
unordered_map<TreeNode*, int> node2depth;
};

日期

2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪

【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)的更多相关文章

  1. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  2. LeetCode 366. Find Leaves of Binary Tree

    原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description 题目: Given a binary tr ...

  3. [leetcode]366. Find Leaves of Binary Tree捡树叶

    Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves ...

  4. 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)

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

  5. 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)

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

  6. LeetCode 606 Construct String from Binary Tree 解题报告

    题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...

  7. LeetCode 104 Maximum Depth of Binary Tree 解题报告

    题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  8. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  9. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

随机推荐

  1. MISA(在线)注释叶绿体基因组SSR

    SSR (Simple Sequence Repeat),即简单重复序列,是一种以PCR技术为核心的DNA分子标记技术,也称为微卫星序列或者串联重复. 简单重复顾名思义就是以很短的序列为一个单元,比如 ...

  2. MariaDB——简介

    一.MariaDB跟MySQL在绝大多数方面是兼容的,对于开发者来说,几乎感觉不到任何不同.是MySQL的代替品. MariaDB虽然被视为MySQL数据库的替代品,但它在扩展功能.存储引擎以及一些新 ...

  3. Python基础之格式化输出的三种方式

    目录 1. 格式化输出的三种方式 1.1 占位符 1.2 format格式化 1.3 f-string格式化 1. 格式化输出的三种方式 在程序中,需要将输出信息打印成固定的格式,这时候就需要格式化输 ...

  4. 学习java的第六天

    一.今日收获 1.开始了学习手册第二章的学习 2.了解了java里的常量与变量以及数据类型,与c语言的内容类似 二.今日难题 1.都是基础知识,没有什么难题 三.明日目标 1.继续学习java学习手册 ...

  5. linux 实用指令压缩和解压类

    linux 实用指令压缩和解压类 目录 linux 实用指令压缩和解压类 gzip/gunzip指令(不常用) zip/unzip指令 tar指令(常用) gzip/gunzip指令(不常用) 说明 ...

  6. 学习java 7.8

    学习内容: 被static修饰的不需要创建对象,直接用类名引用即可 内部类访问特点:内部类可以直接访问外部类的成员,包括私有 外部类访问内部类的成员,必须创建对象 成员内部类,内部类为私有,Outer ...

  7. A Child's History of England.27

    Then, the Red King went over to Normandy, where the people suffered greatly under the loose rule of ...

  8. Java 监控基础 - 使用 JMX 监控和管理 Java 程序

    点赞再看,动力无限.Hello world : ) 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码网站 已经收录,有很多知识点和系列文章. 此篇文 ...

  9. Https原理及证书管理

    Https原理及证书管理 SSL(Secure Sockets Layer,安全套接层)/TLS(Transport Layer Security,传输层安全)保证了客户端web服务器的连接安全.客户 ...

  10. mystar01 nodejs MVC 公共CSS,JS设置

    mystar01 nodejs MVC gulp 项目搭建 config/express.js中定义别名 //将下载的第三方库添加到静态资源路径当中,方便访问 app.use('/jquery', e ...