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

 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>> listAll = new ArrayList<List<Integer>>();
while (root != null) {
List<Integer> list = new ArrayList<Integer>();
root = helper(list, root);
listAll.add(new ArrayList<Integer>(list));
} return listAll;
} private TreeNode helper(List<Integer> list, TreeNode root) {
if (root == null)
return null; if (root.left == null && root.right == null) {
list.add(root.val);
return null;
} root.left = helper(list, root.left);
root.right = helper(list, root.right); return root;
}
}

下面这种方法是不会移除leaves的。

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

Find Leaves of Binary Tree的更多相关文章

  1. [LeetCode] 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: 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. 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. 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 ...

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

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

  7. LeetCode 366. Find Leaves of Binary Tree

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

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

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

随机推荐

  1. JS子父窗口互相取值赋值详解介绍

    子窗口赋值到父窗口 代码如下 复制代码 <script>function openWin(str) {    window.open(siteurl+"popup/"+ ...

  2. java操作excel总结---poi

    前不久做过Excel的导入导出功能,其主要的难点是java如何操作Excel文档.现在就来介绍一下利用Apache的poi如何操作Excel. 1.准备工作:导入Apache POI的相关jar包,P ...

  3. 使用Web Deploy进行远程部署

    Web Deploy支持直接从本地Visual Studio的工程文件部署网站到远程服务器,部署的过程中可以对比哪些文件变化了需要拷贝,而不是一股脑的全部拷贝,效率和准确性会更好. 部署的过程主要要注 ...

  4. hdu1535 SPFA

    2边SPFA 然后求和 #include<stdio.h> #include<string.h> #include<queue> #define INF 10000 ...

  5. Java 工程师的学习线路图。

    今天了一个超级好用的工具,思维导图 FreeMind,于是顺道试用了一下,照着画了一张 Java 工程师的学习线路图.

  6. NOI题库--图论 宗教信仰

    1526:宗教信仰 总时间限制: 5000ms 内存限制: 65536kB 描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 500 ...

  7. hdu 1576 求逆元

    题意:给出n=A mod 9973和B,求(A/B) mod 9973 昨天用扩展欧几里得做过这题,其实用逆元也可以做. 逆元的定义:例如a*b≡1 (mod m),则b就是a关于m的逆元. 求逆元方 ...

  8. NOIP 2012 T5 借教室 [洛谷P1083]

    题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要 向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自 ...

  9. 通知栏Notification的学习

    转:http://blog.csdn.net/yczz/article/details/28416893 在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理 ...

  10. 02 C语言指针

    今天发帖记录自己学习C语言精髓的心理历程,人生就像是一次旅途,沿途总是能看到最美的风景,让我们的思想相逢在C语言中. 一 初识指针,指针的定义 指针是C语言中的一种类型,类似于整形,字符型等.既然C指 ...