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. ~~圣诞节到啦, canvas雪花效果, 漂亮到简直没天理啊~~

    看到coding的主界面有雪花, 原来,哇,  真漂亮, 一看源代码, 哦了个去, angular写的, 压力好大, 分析分析分析分析.... 然后就写成jQ插件的样子给大家用了. 在线预览的页面是: ...

  2. 【探秘ES6】系列专栏(一):ES6简介

    摘要:新一代JavaScript标准,ES6即将发布.[探秘ES6]系列专栏将一一剖析ES6的诸多新特性,让Web开发者对此有清晰全面的了解.本文为系列的第一篇,带你了解ES6到底是什么以及有哪些令人 ...

  3. 【CodeForces 602A】C - 特别水的题3-Two Bases

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/C Description After seeing the ...

  4. oracle 用Navicat创建的表的查询问题

    navicat可视化创建了表,可是就是不能查到!这个为什么呢? select * from user; 我们如果给user加上双引号才能查到 select * from "user" ...

  5. BZOJ-1800 飞行棋 数学+乱搞

    这道题感觉就是乱搞,O(n^4)都毫无问题 1800: [Ahoi2009]fly 飞行棋 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1172 So ...

  6. Springside学习

    http://blog.chinaunix.net/uid-122937-id-3935052.html [一]Maven + Eclipse + springside4安装与配置 Maven安装与配 ...

  7. html中设置锚点定位的几种常见方法(#号定位)

    在html中设置锚点定位我知道的有几种方法,在此和大家分享一下: 1.使用id定位: <a href="#1F">锚点1</a> <div id=&q ...

  8. C/C++代码中的笔误

    1. 在printf()的参数前加& (2015/10/7) 这是我写的一个数据生成器(generator)片段 +; printf("%d\n", &n);

  9. eclipse中使用git

    有的eclipse已经自带了Git了,就不用安装了.如果,想重新安装,可以先卸载GIT,卸载 不同eclipse卸载不一样: 1.在Eclipse中依次点击菜单"Help"-> ...

  10. linux建立用户 详细

    .你同时属于两个或两个以上的组. 两个条件你至少具备一个,你才能够把文件所属旧组变为新组.使用如下的命令将当前目录下所有html文件所属的组改为httpd: chgrp httpd *.html 和c ...