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

题目:

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:
Given binary tree

          1
/ \
2 3
/ \
4 5

Returns [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:

          []         

Returns [4, 5, 3], [2], [1].

题解:

计算当前node到leaf的距离为当前node的高度,相同高度的点放在同一个list里.

Time Complexity: O(n). leaf层用时n/2, leaf上一层用时n/4*2, 再上一层用时n/8*3. 是n(i/2^i)的和. i = 1,2,3....

Space: O(logn), stack space. Regardless res.

AC Java:

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

类似Maximum Depth of Binary Tree.

跟上Boundary of Binary Tree.

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捡树叶

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

  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. 【笔记】IntelliJ IDEA配置Hibernate

    参考:imooc:http://www.imooc.com/video/7706 1.创建Hibernate的配置文件. 将依赖包导入项目.http://blog.csdn.net/a15337525 ...

  2. 【JavaScript】用JS绘制一个球

    参考: 1.http://www.w3school.com.cn/html5/html_5_canvas.asp 2.http://blog.csdn.net/qq_27626333/article/ ...

  3. Saltstack 命令参数整理

    命令:salt-key # 测试 命令:salt-key -A # 管理所有minion 命令:salt-key -a # 管理定义minion 命令:salt-key -d ID名字 # 删除单个m ...

  4. JavaWeb Filter

    1. 过滤器概述 1.1. 什么是过滤器 Filter译为过滤器,是JavaWeb的三大组件之一,用于在Servlet之外对Request或者Response进行修改.对于Web应用程序来说,过滤器是 ...

  5. 周立功CAN-II引脚图

    注意:如果需要两个CAN通道通信,直接用杜邦线将L连L,H连H,地连地就行(3.6引脚已做短接处理),不需要反接

  6. [Android]热修复框架AndFix测试说明

    AndFix,全称是Android hot-fix.是阿里开源的一个热补丁框架,允许APP在不重新发布版本的情况下修复线上的bug.支持Android 2.3 到 6.0,并且支持arm 与 X86系 ...

  7. redis通过命令批量删除key

    需求:想删除 notify_ 开头的所有key redis-cli KEYS "notify_*" | xargs redis-cli DEL 通过 notify_* 来匹配

  8. Java Swing窗体小工具实例 - 原创

    Java Swing窗体小工具实例 1.本地webserice发布,代码如下: 1.1 JdkWebService.java package server; import java.net.InetA ...

  9. Gym - 101503I 利用到图论的构造

    比赛的时候没有注意到 给出的up矩阵 能使我们随便选一列 确定这一列的rank 这样我们得出每一行列的rank 进行构图 大->小 然后从大到小放 当前放的点 和他有因果关系并且比他大的点必须已 ...

  10. Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。

    问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找. 算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法. public class SearchInSortedM ...