题目地址: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. Macbookpro快捷键

    Alt键代替 Option 键,用 Windows 标志键代替 Command 键. 但是,使用的时候,Command健类似于Windows的Control键. 1.复制.粘贴.剪切.全选.查找 Co ...

  2. Xpath解析库的使用

    ### Xpath常用规则 ## nodename 选取此节点的所有子节点 ## / 从当前节点选取直接子节点 ## // 从当前节点选取子孙节点 ## . 选取当前节点 ## .. 选取当前节点的父 ...

  3. 18-Rotate Array-Leetcode

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  4. kubernetes部署kube-scheduler服务

    同样的分非认证授权和认证授权: 非认证授权: cat > /lib/systemd/system/kube-scheduler.service <<EOF [Unit] Descri ...

  5. ui自动化测试,页面方法的使用

    悬浮下拉框 的设置选择 下拉框的选择 显性等待 双击, ActionChains类的方法行动链 提示框 双击,右击 双击用到行动连,提示框用到Alert的类 右击用到的也是行动连 UI自动化测试 #h ...

  6. academy

    academy at/in school都行,academy一般用at. The word comes from the Academy in ancient Greece, which derive ...

  7. HTTP请求 Java API

    1.导入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId>common ...

  8. Gradle入门及SpringBoot项目构建

    https://blog.csdn.net/qq_27520051/article/details/90384483 一.介绍 Gradle 是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是 ...

  9. show processlist命令详解

    1.show processlist; SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到 ...

  10. vue2.x入门学习

    vue安装 # 最新稳定版本 $ npm install vue # 最新稳定 CSP 兼容版本 $ npm install vue@csp 引包 cd /d/vue/demo cnpm instal ...