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:

          []         

题意:

逐层移除二叉树的叶节点。

Solution1: DFS

解本题需要的背景知识: 【height】The height of a node is the number of edges from the node to the deepest leaf.

For instance,

node 1 has height of 2
node 2 has height of 1
node 4 has height of 0
node 5 has height of 0
node 3 has height of 0
Based on the output, we need to gather up all the nodes with height 0, we then gather up all the nodes with height 1, and then all the nodes with height 2

所以我们的target就是边traversal given tree边拿到each node's height, 把不同height的node放到result里

code

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
helper(res, root);
return res;
}
//helper function to return the height of current node
private int helper(List<List<Integer>> res, TreeNode root) {
if (root == null) return -1;
int left = helper(res, root.left);
int right = helper(res, root.right);
int height = Math.max(left, right) + 1;
if (res.size() == height) {
res.add(new ArrayList<>());
}
res.get(height).add(root.val);
return height;
}
}

[leetcode]366. Find Leaves of Binary Tree捡树叶的更多相关文章

  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 lea ...

  4. 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)

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

  5. 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 ...

  6. 366. Find Leaves of Binary Tree C#

    Example:Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Removing th ...

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

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. Linux初级入门(第一次作业)

    Linux初级入门 在本科期间学过一些Linux的简单命令,再次接触Linux不仅巩固了知识还学习到了很多新的东西. 什么是操作系统? 操作系统,英文名称Operating System,简称OS,是 ...

  2. Python手记(二)

    1.map函数 map函数用于将指定的数据成员都使用指定函数进行处理. 比如: map(float, arr) map(square, arr) 这两个函数分别将arr中成员转换为float类型,以及 ...

  3. Eclipse+PyDev 安装和配置

    Eclipse+PyDev 安装和配置 2012-03-17 23:23:23 | 17465次阅读 | 评论:0 条 |  Python开发有很多工具,其中Eclipse+Pydev 是最常见的一种 ...

  4. 网络之OSI七层协议模型、TCP/IP四层模型

    13.OSI七层模型各层分别有哪些协议及它们的功能 在互联网中实际使用的是TCP/IP参考模型.实际存在的协议主要包括在:物理层.数据链路层.网络层.传输层和应用层.各协议也分别对应这5个层次而已. ...

  5. 当一个HTML元素需要添加mouseon、mouseout与click事件,或者mouserenter、mouseleave和click事件时,click事件无法触发

    当一个HTML元素需要添加mouseon.mouseout与click事件,或者mouserenter.mouseleave和click事件时,click事件无法触发 针对上述问题,我遇到的有两种情况 ...

  6. Inception-v3的设计思路小结

    一.网络更深.更宽带来的问题 参数太多,若训练数据集有限,容易过拟合: 网络越大计算复杂度越大,难以应用:(内存和计算资源) 网络越深,梯度越往后穿越容易消失,难以优化模型. 解决: 如何减少参数(且 ...

  7. 自己动手制作的淘宝闲鱼APP宝贝数据采集工具软件

    之前做过淘宝PC端宝贝和店铺数据的采集,后来需要做APP端的数据采集,因为没有学过Android,以前也都是做PC端的软件,有没有其他方法呢? 突然想到了用手机模拟器,可以在电脑端控制运行手机APP端 ...

  8. Oracle 学习笔记(五)

    --表空间,auto: 自动管理, manual: 手动管理   create tablespace  tsp1 datafile 'D:\ORACLE\ORADATA\O10\tsp1.dbf'   ...

  9. oracle入坑日记<五>数据表

    1   数据表 1.1.数据表是存放数据字段信息的地方:在Oracle,数据表拥有者单位是用户,同时数据表属于表空间.如: 登录my_user用户在orcl表空间下创建的表就是 my_user用户在o ...

  10. 63.1拓展之纯 CSS 创作一个摇摇晃晃的 loader

    效果地址:https://scrimba.com/c/cqKv4VCR HTML code: <div class="loader"> <span>Load ...