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. Linux操作系统学习_操作系统是如何工作的

    实验五:Linux操作系统是如何工作的? 学号:SA1****369 操作系统工作的基础:存储程序计算机.堆栈(函数调用堆栈)机制和中断机制 首先要整明白的一个问题是什么是存储程序计算机?其实存储程序 ...

  2. shell脚本获取随机数random

    用C提供的取随机数的方法srand和rand, 前者是给后者设置随机数种子seed. ; srand(seed); // time(NULL) 通常使用时间做种子 rnd_num = rand(); ...

  3. Java-调用抽象类中指定参数的构造方法

    abstract class person {  private String name;  private int age;  public person(String name,int age) ...

  4. Android应用--简、美音乐播放器获取专辑图片(自定义列表适配器)

    Android应用--简.美音乐播放器获取专辑图片(自定义列表适配器) 2013年7月3日简.美音乐播放器开发 第二阶段已增加功能: 1.歌词滚动显示 2.来电监听 3.音量控制 4.左右滑动切换歌词 ...

  5. Mybatis原理图

    Mybatis原理图 MyBatis 是一个基于Java的持久层框架.它提供的持久层框架包括SQL Maps和Data Access Objects(DAO). MyBatis 是支持普通 SQL查询 ...

  6. memcached缓存技术

    初学memcached缓存技术,如果文章写得不好还请谅解 应用环境:win7 实现环境:cmd,eclipse Memcached简洁而强大.它的简洁设计便于快速开发,减轻开发难度,解决了大数据量缓存 ...

  7. MVC源码解析 - Http Pipeline 解析(上)

    IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(context); 上一篇说到了创建 ...

  8. 【Java每日一题】20170113

    20170112问题解析请点击今日问题下方的"[Java每日一题]20170113"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...

  9. hdu1041

    #include <iostream> #include <string> using namespace std; const int SIZE = 1001; const ...

  10. shell 分支/循环

    ==)); then patern="Update" else patern="Read" fi in "-h") ] then helpI ...