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 ...
随机推荐
- service structure flowchart [mobile to server via TCP/IP protocol]
For instant message delivering
- 理解git对象
1. 首次提交,提交一个简单的文件 a.txt ,commit 之后的图如下: 如图所示,生成了 3 个对象,一个 commit 对象,一个 tree 对象,一个 blob 对象.图上蓝底是 co ...
- jquery代码实现简单的五星评价功能!
实现: 1,鼠标移动到第三个星星,则一二三星星变亮,后两个变暗 2,鼠标点击某个星星后,可以继续选择,但拿开后星星会定格住你点击的位置 <script type="text/javas ...
- 结构-行为-样式-Angularjs-ngSanitize
简单点,上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 快速排序Java版
package Quick; public class quicksort { static class QuickSort { public int data[]; private int part ...
- [jQuery]on和bind事件绑定的区别
on和bind事件绑定的区别 一个demo展示 <!DOCTYPE html> <html lang="zh"> <head> <titl ...
- import,include
1. #import导入头文件,即:导入头文件中的内容到当前类 2. #import ""导⼊自定义类,#import <>导入类库中的头文件. 3.功能类似C语言中的 ...
- 基于Unity的Profiler性能分析
A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体时间-所有单项的记录时 ...
- C# ObjectHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- js导航栏样式变换
<script type="text/javascript"> $(function(){ var lis = $(".submenu").chil ...