原题链接在这里: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. session/token/cookie/socket区别

    Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象 ...

  2. Batch Normalization 详解

    一.背景意义 本篇博文主要讲解2015年深度学习领域,非常值得学习的一篇文献:<Batch Normalization: Accelerating Deep Network Training b ...

  3. 并发-HashMap和HashTable源码分析

    HashMap和HashTable源码分析 参考: https://blog.csdn.net/luanlouis/article/details/41576373 http://www.cnblog ...

  4. windchill系统——开发_生命周期状态的增加

    步骤如下 ResourceBuild wt.lifecycle.StateRB ant -f codebase/MakeJar.xml 这样就可以了,下面是查看

  5. 4.微信小程序-B站:先把首页造出来

    (小安娜:失踪人口已上线,大家快来喷喷喷他!),sorry++,最近身边发生太多事情,导致这最关键的实战开篇都未写,(小安娜-分身1:懒就是懒,不负责任,我之前学的都忘了),(小安娜-分身2:上一篇双 ...

  6. Dynamo概述

    Dynamo 是Amazon提供的一款高可用的分布式Key-Value存储系统,其满足可伸缩性.可用性.可靠性. CAP原理满足:通过一致性哈希满足P,用复制满足A,用对象版本与向量时钟满足C.用牺牲 ...

  7. Coundn't load memtrack module (No such file or directory)

    Coundn't load memtrack module (No such file or directory) 去仔细看日志,是包名有问题 一.出现症状 提示找logcat logcat里面发现C ...

  8. ubuntu16.04 添加中文ibus输入法

    ubuntu版本 16.04 在terminal  输入命令 sudo apt-get install ibus-pinyin sudo apt-get ibus-setup 设置 选择拼音,添加选择 ...

  9. 三十三 Python分布式爬虫打造搜索引擎Scrapy精讲—数据收集(Stats Collection)

    Scrapy提供了方便的收集数据的机制.数据以key/value方式存储,值大多是计数值. 该机制叫做数据收集器(Stats Collector),可以通过 Crawler API 的属性 stats ...

  10. Go语言实现FastDFS分布式存储系统WebAPI网关

    前言 工作需要,第一次使用 Go 来实战项目. 需求:采用 golang 实现一个 webapi 的中转网关,将一些资源文件通过 http 协议上传至 FastDFS 分布式文件存储系统. 一.Fas ...