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

Solution:

Mark tree by level , if it is leave then mark as level 0, then add to the List<List<int>> by level.

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public IList<IList<int>> FindLeaves(TreeNode root) {
IList<IList<int>> result = new List<IList<int>> ();
LeavesLevel(root, result);
return result;
} public int LeavesLevel(TreeNode root, IList<IList<int>> result)
{
if(root==null)
{
return -;
}
int leftLevel = LeavesLevel(root.left, result);
int rightLevel = LeavesLevel(root.right, result);
int level = Math.Max(leftLevel, rightLevel)+;
if(result.Count()<level+)
{
result.Add(new List<int>());
}
result[level].Add(root.val);
return level;
}
}

366. Find Leaves of Binary Tree C#的更多相关文章

  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输出层数相同的叶子节点

    [抄题]: Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all ...

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

随机推荐

  1. Java笔记:与系统交互、系统相关的类,Object类

    1.程序与用户交互 (1)运行一个Java程序的时候要给它提供一个main方法入口,这边分析一下这个main方法的签名public static void main(String[] args);pu ...

  2. jQuery Mobile (整合版)

    jQuery Mobile (整合版) 前言 为了方便大家看的方便,我这里将这几天的东西整合一下发出. 里面的例子请使用手机浏览器查看. 什么是jQuery Mobile? jquery mobile ...

  3. 关于C++中Object所占内存空间探索1

    关于C++中Object所占内存空间探索(一) 有如下问题: 1. 一个空类, class X{ }; 2.类中含有数据成员(Data Member), class X { public: //Fun ...

  4. 学习OpenCV:滤镜系列(15)——羽化(模糊边缘)

    ============================================== 版权所有:小熊不去实验室CSDN博客 ================================== ...

  5. 在 ML2 中配置 OVS vlan network - 每天5分钟玩转 OpenStack(136)

    前面我们已经学习了 OVS 的 local 网络 和 falt 网络,今天开始讨论 vlan 网络. vlan network 是带 tag 的网络. 在 Open vSwitch 实现方式下,不同 ...

  6. 在html页面中展示JSON

    背景: 有时候我们需要将json数据直接显示在页面上(比如在做一个接口测试的项目,需要将接口返回的结果直接展示),但是如果直接显示字符串,不方便查看.需要格式化一下. 解决方案: 其实JSON.str ...

  7. 制作类似ThinkPHP框架中的PATHINFO模式功能(二)

    距离上一次发布的<制作类似ThinkPHP框架中的PATHINFO模式功能>(文章地址:http://www.cnblogs.com/phpstudy2015-6/p/6242700.ht ...

  8. JavaScript构造函数+原型创建对象,原型链+借用构造函数模式继承父类练习

    虽然经常说是做前端开发的,但常常使用的技术反而是JQuery比较多一点.在JavaScript的使用上相对而言少些.尤其是在创建对象使用原型链继承上面,在项目开发中很少用到.所以今天做个demo练习一 ...

  9. (二)Harbor WEB的使用

    接上一篇<安装Harbor>,安装好之后,接下来我们就进行Harbor  web界面的操作吧! 转载请标明出处:http://www.cnblogs.com/huangjc/p/62704 ...

  10. package com.sun.image.codec.jpeg does not exist

    rt.jar 是sun公司内部使用的包,不建议外部使用,rt.jar是被用了的,但是里面的com.sun下面的包不被默认加载了, JAVA7之前是默认加载的,所有用JAVA7以前的JDK编译是通过的. ...