题目地址: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. 系统发育树邻接法(NJ)和非加权组平均法(UPGMA)之比较

    目录 1.原理的区别 2.实操比较 UPGMA NJ法 保存树文件 更深理解 1.原理的区别 主要区别在于,非加权组平均法(UPGMA)是基于平均链接方法的聚集层次聚类方法,而邻接法(NJ)是基于最小 ...

  2. 【3】蛋白鉴定软件之Mascot

    目录 1.简介 2.配置 2.1在线版本 2.2 服务器版本 3.运行 3.1 在线版本 3.2 服务器版本 4.结果 1.简介 Mascot是非常经典的蛋白鉴定软件,被Frost & Sul ...

  3. CentOS6.9安装python3

    安装依赖包: yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel w ...

  4. CSS上下左右居中对齐

    上下左右居中对齐 display:  inline/inline-block 将父元素(容器)设定 text-align: center: 即可左右置中. display: block 将元素本身的 ...

  5. 学习java 7.14

    学习内容: 标准输入输出流 输出语言的本质:是一个标准的输出流 字节打印流 字符打印流 对象序列化流 明天内容: 进程和线程 遇到问题: 用对象序列化流序列化一个对象后,假如我们修改了对象所属的类文件 ...

  6. 零基础学习java------day14-----泛型,foreach,可变参数,数组和集合间的转换,Set,Map,

    1.泛型(jdk1.5以后出现) https://www.cnblogs.com/lwbqqyumidi/p/3837629.html#!comments (1)为什么要用泛型? 限制集合,让它只能存 ...

  7. deque、queue和stack深度探索(下)

    deque如何模拟连续空间?通过源码可以看到这个模型就是通过迭代器来完成. 迭代器通过重载操作符+,-,++,--,*和->来实现deque连续的假象,如上图中的 finish-start ,它 ...

  8. js处理title超长问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. 学习Oracle遇到的实际问题(持续更新)

    有三个用户参与这个事情: system用户,拥有表manager. sys create了一个用户item,并赋予权限: SQL> GRANT SELECT ON SYSTEM.MANAGER ...

  10. Oracle SQL中join方式总结

    在ORACLE数据库中,表与表之间的SQL JOIN方式有多种(不仅表与表,还可以表与视图.物化视图等联结).SQL JOIN其实是一个逻辑概念,像NEST LOOP JOIN. HASH JOIN等 ...