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:

Input: [1,2,3,4,5]

          1
/ \
2 3
/ \
4 5 Output: [[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:

          []         

题意:

逐层移除二叉树的叶节点。

Solution1: DFS

解本题需要的背景知识: 【height】The height of a node is the number of edges from the node to the deepest leaf.

For instance,

node 1 has height of 2
node 2 has height of 1
node 4 has height of 0
node 5 has height of 0
node 3 has height of 0
Based on the output, we need to gather up all the nodes with height 0, we then gather up all the nodes with height 1, and then all the nodes with height 2

所以我们的target就是边traversal given tree边拿到each node's height, 把不同height的node放到result里

code

 /**
* 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) {
List<List<Integer>> res = new ArrayList<>();
helper(res, root);
return res;
}
//helper function to return the height of current node
private int helper(List<List<Integer>> res, TreeNode root) {
if (root == null) return -1;
int left = helper(res, root.left);
int right = helper(res, root.right);
int height = Math.max(left, right) + 1;
if (res.size() == height) {
res.add(new ArrayList<>());
}
res.get(height).add(root.val);
return height;
}
}

[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

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

  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. php配置php_pdo_mysql模块

    网上的都是什么编译安装的,总算找到一个简单的方法 安装好PHP yum install php php-fpm -y 直接安装pdo模块 yum install php-pdo_mysql 在/etc ...

  2. C# ZipHelper C#公共类 -- ZipArchive实现压缩和解压

    从网上找来个ZipArchive来压缩和解压缩的类,供参考吧 /******************************************************************** ...

  3. 互联网同步yum服务器,中科大 rsync createrepo

    参考文章 https://blog.csdn.net/chenjia6605/article/details/82734945 1.本机安装所需工具: yum -y install rsync cre ...

  4. VUE中如何优雅的动态绑定长按事件

    答案没有: 图片是从后端传过来, 加到imgTarget属性,实现长按点击删除该图片 let img = document.createElement('img'); img.src = " ...

  5. Ubuntu16.04安装Truffle和TestRPC

    系统环境 Ubuntu16.04;   NodeJS: v6.10.2;    NPM: 3.10.10: Truffle: 2.0.8; TestRPC: 3.0.5 安装步骤 注意:以root用户 ...

  6. startup.bat 一闪而过解决方案

    原文地址:http://blog.csdn.net/stypace/article/details/38083581 启动tomcat时cmd窗口一闪而过解决方法. 注: 我这边只是配置了环境变量就o ...

  7. synchronized 实现同步的基础

    1.普通同方法,锁是当前实例对象 2.静态同步方法,锁是当前类的class对象 3.同步代码块,锁是括号里的对象

  8. java之try、catch、finally

    结论:try和catch相当于程序分支,finally块中不会改变变量的指针(引用地址):和final修饰的变量类似. public class Test { public static AreaRQ ...

  9. django 补充和中间件

    配置 from django.conf import settings form组件 from django.forms import Formfrom django.forms import fie ...

  10. 性能测试day03_前端分析调优思路

    刚刚看到有人支持我写的博客,表示还是比较感动的,发现热心的用户在我的博客留言说“一个系统每天有200万在线用户,问我怎么设计性能场景?”,其实这个问题呢就属于业务没理清,这个问题就像我问你,一个城市一 ...