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 ...
随机推荐
- Web软件开发工具WebBuilder试用手记
最近公司在使用WebBuilder做项目开发,感觉很不错. 官方主页在这里:http://www.putdb.com/ 可以看到,这货不仅能使用可视化的方式拖拽出界面,还能直接在页面上完成数据库相关的 ...
- JS中for循序中延迟加载实现动态效果
JS中for循序中延迟加载实现动态效果 今天在做一个前端的效果的时候碰到一个棘手的问题,就是实现一个动态的圆柱效果,废话不多少,直接上代码. <script src="js/jquer ...
- .NET程序集1
谈谈.NET程序集(一) 谈谈.NET程序集(一) The Assembly in .NET by 唐小崇 http://www.cnblogs.com/tangchong 在.NET出现之前, Wi ...
- elasticsearch data importing
ElasticSearch stores each piece of data in a document. That's what I need. Using the bulk API. Trans ...
- python实现基于CGI的Web应用
python实现基于CGI的Web应用 本文用一个“网上书店”的web应用示例,简要介绍如何用Python实现基于CGI标准的Web应用,介绍python的cgi模块.cigtb模块对编写CGI脚本提 ...
- IE11仿真文档模式默认IE5 IE7的调整办法
<meta http-equiv="X-UA-Compatible" content="IE=edge">
- linux培训笔记1
第五章 文件和目录的管理 linux命令的基本格式 #命令 [选项] [参数] 1.linux下的常用命令 (1)ls 查看(列出)目录下的内容 -l 查看文件详 ...
- kubernetes入门之快速部署
角色说明 这里主要有三个角色,分别部署不同的服务. 角色 服务 etcd etcd master kube-apiserver/kube-scheduler/kube-controller node ...
- Docker容器内存监控
linux内存监控 要明白docker容器内存是如何计算的,首先要明白linux中内存的相关概念. 使用free命令可以查看当前内存使用情况. [root@localhost ~]$ free tot ...
- JVM执行引擎的执行过程
摘自深入分析java web技术内幕