[抄题]:

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. hsdfz -- 6.16 -- day1

    恩这回不写游记了 按照老师要求记录今天的心里路程:这题似乎可做期望得分150->日部分分似乎不是很显然->a题似乎是结论题,大力猜一波结论->过不了样例,先看b题->b题动态树 ...

  2. C# Android 开发中使用 Sqlite.NET ORM

    开发环境:VS2015 Xamarin Sqlite.NET ORM 不就相当于 Entiry Framework For Xamarin 吗? 相当于用 C# 开发安卓程序访问 Sqlite 可以使 ...

  3. socat管理haproxy以及haproxy调优

    Unix套接字命令(Unix Socket commands) socat是一个多功能的网络工具,名字来由是“Socket CAT”,可以看作是netcat的N倍加强版,socat的官方网站:http ...

  4. bootstrapValidator关于js,jquery动态赋值不触发验证(不能捕获“程序赋值事件”)解决办法

    //触发oninput事件 //propertychange 兼容ie678 $('#captainName').on('input propertychange', function() { }); ...

  5. css零碎知识点小结

    1.单行文字溢出显示省略号: div{ width: 200px; overflow: hidden; text-overflow:ellipsis; white-space: nowrap; } 2 ...

  6. MongoDB查询优化

    项目场景:Mongo在首次查询特慢,后面就好的.如果长时间不查询,下次开始的第一次又将非常慢,于是从链接当时多方面,排查最终发现还是mongo索引建的有问题. MongoDB在大批量数据查询时经常会遇 ...

  7. (转)查看SQLServer最耗资源时间的SQL语句

    原文地址:https://www.cnblogs.com/My-Dream/p/6270308.html 1.找出执行时间最长的10条SQL(适用于SQL SERVER 2005及其以上版本) SEL ...

  8. tkinter 写一个简易的ide

    简易IDE 基于tkinter的简易ide,参考文档和Baidu的资料制作的,过程中遇到了很多问题,也学到了很多知识. 功能: 1.菜单栏 2.编辑功能 open save... 3.快捷键  ctr ...

  9. python第三方库Requests的基本使用

    Requests 是用python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...

  10. 【笔记】range函数在py3里面的处理及numpy库效率比较【原创】

    今天看了一下,numpy数组操作其中一段代码,主要是测试用纯python和numpy之间的性能问题 在py2环境下,代码如下: def pysum(n): a = range(n) b = range ...