原题链接在这里: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. WINDOWS和UNIX换行符的理解

    # WINDOWS和UNIX换行符的理解 **file1.txt**17.143.161.37   其他    美国54.163.255.40   其他    美国 弗吉尼亚州 亚马逊公司 **[ro ...

  2. [国家集训队] calc(动规+拉格朗日插值法)

    题目 P4463 [国家集训队] calc 集训队的题目真是做不动呀\(\%>\_<\%\) 朴素方程 设\(f_{i,j}\)为前\(i\)个数值域\([1,j]\),且序列递增的总贡献 ...

  3. springmvc 整合微信

    springmvc 整合微信 方式一: ① 配置验证 @RequestMapping(value = "/into", method = RequestMethod.GET, pr ...

  4. [POI2009]Lyz

    Description 初始时滑冰俱乐部有1到n号的溜冰鞋各k双.已知x号脚的人可以穿x到x+d的溜冰鞋. 有m次操作,每次包含两个数ri,xi代表来了xi个ri号脚的人.xi为负,则代表走了这么多人 ...

  5. StrStr,判断一个字符串是不是另一个字符串的字串,并返回子串的位置

    public int strStr(String haystack, String needle) { if(haystack == null || needle == null) { return ...

  6. iSCSI 在Linux下的模拟实验

    5.iSCSI客户端(Initiator)配置 在Linux 2.6内核中提供了iscsi驱动,iSCSI 驱动(driver)使主机拥有了通过IP网络访问存储   的能力,驱动在主机(Initiat ...

  7. 报错org.apache.ibatis.binding.BindingException: Type interface com.atguigu.mybatis.bean.dao.EmployeeMapper is not known to the MapperRegistry.

    报错org.apache.ibatis.binding.BindingException: Type interface com.atguigu.mybatis.bean.dao.EmployeeMa ...

  8. MySQL二进制日志功能介绍

    二进制日志记录所有更新数据的SQL语句,其中也包含可能更新数据的SQL语句,例如DELETE语句执行过程中无匹配的行.二进制日志中还包含了与执行SQL语句相关的内容,例如SQL语句执行的时间.错误代码 ...

  9. IT技术栈、JAVA技术栈、游戏开发技术栈

    一.形成IT思想,把各种技术融会贯通,使用时按需对技术选型. 二.对于每个知识点,框架的掌握依次分为三层. 1.会使用 2.熟悉原理 3.了解源码 三.思维导图

  10. spring3: 内置Resource实现

    4.2  内置Resource实现 4.2.1  ByteArrayResource ByteArrayResource代表byte[]数组资源,对于“getInputStream”操作将返回一个By ...