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 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#的更多相关文章
- 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 ... 
- 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 ... 
- [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 ... 
- LeetCode 366. Find Leaves of Binary Tree
		原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description 题目: Given a binary tr ... 
- [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 ... 
- 【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 ... 
- 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ... 
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
		Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ... 
- 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 ... 
随机推荐
- 简单使用JSON,通过JSON 字符串来创建对象(二)
			把 JSON 文本转换为 JavaScript 对象 JSON 最常见的用法之一,是从 web 服务器上读取 JSON 数据(作为文件或作为 HttpRequest),将 JSON 数据转换为 Jav ... 
- Mathematics for Computer Graphics
			Mathematics for Computer Graphics 最近严重感觉到数学知识的不足! http://bbs.gameres.com/showthread.asp?threadid=105 ... 
- 大数据应用日志采集之Scribe演示实例完全解析
			大数据应用日志采集之Scribe演示实例完全解析 引子: Scribe是Facebook开源的日志收集系统,在Facebook内部已经得到大量的应用.它能够从各种日志源上收集日志,存储到一个中央存储系 ... 
- PHP使用DomDocument抓取HTML内容
			有时候会有需要从一个HTML页面来分离出你需要的一些数据来进行处理. 当然自己分析文件肯定可以,但是比较快速且方便的是使用正则表达式或者DOM. 鉴于正则表达式我不熟悉,所以我打算使用DOM来完成. ... 
- php中的foreach函数
			Foreach 函数(PHP4/PHP5) foreach 语法结构提供了遍历数组的简单方式. foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信 ... 
- C++中的结构体vector排序
			在包含了头文件#include <algorithm>之后,就可以直接利用sort函数对一个vector进行排序了: // sort algorithm example #include ... 
- jQuery 怎么实现文字显示2s,消失0.5s,再显示2s,再消失0.5s,以此循环
			<div style="display: none;" id='divTestDisplay'>我要显示的文字</div> window.onload = ... 
- Finding Lines
			Finding Lines 题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ... 
- 不要错过 DevOps 之父出席的2017 DevOpsDays 北京站!
			通过 DevOpsDays 活动,Patrick 将 DevOps 这项伟大的运动带到了地球的东方,带到了北京.同时,他将亲自参加2017年3月18日的 DevOpsDays 北京站,并作精彩演讲. ... 
- 主成分分析  R语言
			主成分分析(Principal Component Analysis,PCA), 是一种统计方法.通过正交变换将一组可能存在相关性的变量转换为一组线性不相关的变量,转换后的这组变量叫主成分. 原理: ... 
