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. OpenSSL-Win32,rsa,私钥,公钥,1024,2048

    默认是rsa_private_key1024.pem , PEM格式私钥,C# ,PHP 用. 再生成 pkcs8 格式私钥, JAVA 用. 公钥无格式区分. 1024 的: openssl.exe ...

  2. 第一个Eureka程序,Eureka Client的自启动原理和简要过程

    https://blog.csdn.net/u011531425/article/details/81675289 在之前的Spring Cloud Config的基础上,搭建简单的Eureka Se ...

  3. mysql 拒绝登录解决

    一大早打开Navicat Lite for MySQL客户端,提示1045 access denied for user ’root’@’localhost’ using password yes,太 ...

  4. profile default

    SAPDBHOST = 10.199.0.26 j2ee/dbtype = hdb j2ee/dbname = ISD j2ee/dbhost = 10.199.0.26 dbs/hdb/dbname ...

  5. linux 查看端口被占用

    linux 查看端口被占用 1.lsof  -i : 端口号 用于查看某一端口的占用情况,比如查看8080端口使用情况,lsof  -i:8080 如果执行 lsof  -i:8080 系统提示 :  ...

  6. java.lang.NoClassDefFoundError: org/apache/tomcat/util/res/StringManager

    一个比较老的web项目,  IDEA 导入后不能用,  出现了各种问题, 但是, 别人用eclipse 导入就不会有问题,  我折腾了半天, 还是各种问题,  真是郁闷了.  哎, 承认很难配置吧, ...

  7. PHP响应码和HTTP请求方法

    HTTP请求报文由请求行(request line).请求头部(header).空行和请求数据4个部分组成,格式如下 可见请求行由请求方法字段.URL字段和HTTP协议版本字段3个字段组成,它们用空格 ...

  8. JS截取URL地址参数

    var url = window.location.search; 截取?r= 后面的参数var url = window.location.href;var urlss= urlssplit('co ...

  9. html2canvas html截图插件

    以下我总结了一些注意事项,在代码中注释了,仅供参考. html2canvas.js点击付:完整使用的demo ,如下: <!DOCTYPE html><html lang=" ...

  10. sqlserver float小数存数据库变成多位了 比如说12.23存进去变成 12.229999998 甚至更长

    使用 numeric(12,2)的数据类型,或者decimal(12,2)   追问 不能随意修改表结构 有别人办法么 程序上控制的 追答 那你就不用管他了,所谓 浮点数,必然是这么存储的.