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, 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的题,详见此图,请理解:

[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
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输出层数相同的叶子节点的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- LeetCode 366. Find Leaves of Binary Tree
原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description 题目: Given a binary tr ...
- [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 ...
- 【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 ...
- 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 获取所有树叶子节点 注册添加事件 if ($(node).tree('isLeaf', node.target)) 是否叶子节点
//获取所有树叶子节点 注册添加事件 if ($(node).tree('isLeaf', node.target)) 是否叶子节点 $(function () { $('.easyui-tree') ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- Python数据类型的可变与不可变
首先,我们需要知道在python中哪些是可变数据类型,哪些是不可变数据类型.可变数据类型:列表list和字典dict:不可变数据类型:整型int.浮点型float.字符串型string和元组tuple ...
- 项目管理软件 GanttProject 节日表
项目管理软件 GanttProject 节日表 GanttProject 是一款项目管理软件,对比一些商业项目管理软件一点不差,有些还更强大,比如以下的节假日自动选择. 文件地址在:C:\Progra ...
- 各CF-based tracker中output_sigma_factor取值
现有的各CF-Based tracker中理想高斯响应中output_sigma_factor的取值情况 默认output_sigma = target_sz*output_sigma_factor; ...
- Html 页面载入内容前,显示 loading 效果。
Html 内容 loading部分: <div id="sys-loading" class=""><div class="spin ...
- VS Code 运行 TypeScript 操作指南
总结一下TypeScript开发环境用到的各种工具: Node——通过npm安装TypeScript及大量依赖包.从https://nodejs.org/下载并安装它:如果安装各种包不方便,可以将安装 ...
- [转][C#]压缩解压
{ internal static class Compressor { public static Stream Decompress(Stream source, bool bidiStream) ...
- KPPW2.5 漏洞利用--CSRF
kppw2.5 CSRF漏洞复现 漏洞说明 http://192.168.50.157/kppw25/index.php?do=user&view=message&op=send 收件 ...
- 学习 MeteoInfo二次开发教程(六)
在教程(五)的基础上加了Faded,Grid_Fill,Grid_Point,Raster,Vector,Barb,Streamline 1.同样注意修改LegendStyleEnum改为Legend ...
- 学习 MeteoInfo二次开发教程(四)
教程四的问题不大. 1.private void AddMapFrame_ChinaSouthSea().private void AddTitle()两个函数和public Form1()函数并列. ...
- (整理)SQL Server 2008 CDC 功能使用
最近某项目突然要增加数据的获取,但是不能改程序.也没有同步的只读库,只好使用CDC来进行尝试. CDC的启用和停止全部用SQL实现,在这里给出主要的SQL步骤: /****** Script for ...