[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

root为空

[思维问题]:

没看出来是dc-depth的题,详见此图,请理解:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. res.get(level).add(node.val);可以实现精确添加,指定哪一层添加哪个数

[二刷]:

双层数组的添加,直接.add(new ArrayList<>());就行了 不需要指定名字

helper(result, root); int函数调用的时候可以没有返回值,直接用

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* 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) {
//ini
List<List<Integer>> result = new ArrayList<List<Integer>>(); //cc
if (root == null) return result; //call the helper function
depthHelper(result, root); //return
return result;
} public int depthHelper(List<List<Integer>> result, TreeNode root) {
//cc: root == null
if (root == null) return -1; //get depth
int depth = 1 + Math.max(depthHelper(result, root.left), depthHelper(result, root.right)); if (depth + 1 > result.size()) result.add(new ArrayList<Integer>()); //add root.val
result.get(depth).add(root.val); return depth;
}
}

366. Find Leaves of Binary Tree输出层数相同的叶子节点的更多相关文章

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

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

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

  4. LeetCode 366. Find Leaves of Binary Tree

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

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

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

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

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

  8. 获取所有树叶子节点 注册添加事件 if ($(node).tree('isLeaf', node.target)) 是否叶子节点

    //获取所有树叶子节点 注册添加事件 if ($(node).tree('isLeaf', node.target)) 是否叶子节点 $(function () { $('.easyui-tree') ...

  9. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

随机推荐

  1. 关于动态内存malloc和realloc

    1.malloc   1.申请的内存长度可以运行时决定,单位是字节  2.申请的内存为连续的内存空间  3.返回的地址可以根据实际需要强转成对应的类型  4.动态申请内存的生命周期是整个程序,除非手动 ...

  2. 使用JAVA实现的一个简单IOC注入实例

    https://blog.csdn.net/echoshinian100/article/details/77977823 欲登高而望远,勿筑台于流沙 RSS订阅 原 使用JAVA实现的一个简单IOC ...

  3. laravel框架容器管理

    来自http://www.cnblogs.com/chy1000/p/7072936.html 本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章 ...

  4. Linux系统目录权限chmod误操作权限修复方法

    Linux中,如果意外误操作将/目录权限批量设置,比如chmod -R 777 / ,系统中的大部分服务以及命令将无法使用,这时候可以通过系统自带的getfacl命令来拷贝和还原系统权限,若是其他系统 ...

  5. 半分钟学会使用markdown基本语法

    想看文字版的看这个吧https://www.jianshu.com/p/191d1e21f7ed

  6. Tomcat性能调优后, 启动出现警告问题 [did not find a matching property.]

    http://blog.csdn.net/dracotianlong/article/details/8963594 Tomcat性能调优后, 启动出现警告问题 [did not find a mat ...

  7. centos 7中监控mysql 数据库脚本(监控端口)

    centos 7中监控mysql 数据库脚本(监控端口) 监控mysql数据库的方法如下: 1.监控端口 netstat -nltp |grep 3306 2.监控进程 ps -ef |grep 33 ...

  8. Android Dialog.dismiss()与Activity.finish()顺序

    activity.finish() 和dialog.show() 同时调用的时候, 需要先调用dialog.dismiss() 后activity.finish() 如果先直接finish()后,再触 ...

  9. (整理)EF分页的实现

    最近做一个小功能,需要数据分页,因为小框架使用的是EF,因此查询了一下EF的分页. EF分页主要用到了skip和take两个方法: GetListBy(lamda xxxxx).skip(PageSi ...

  10. 支持flash in Chrome 2017

    在设置页面: chrome://settings/content 修改Flash插件的使用方式为:Allow sites to run Flash 来源: https://helpx.adobe.co ...