/**
* 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 {
Queue<TreeNode> Q = new Queue<TreeNode>(); private List<TreeNode> FloorSearch()
{
var floor = new List<TreeNode>(); while (Q.Count > )
{
var node = Q.Dequeue();//出来一个节点,放入其左右子节点
//将当前节点放入floor中
floor.Add(node);
} foreach (var f in floor)
{
if (f.left != null)
{
Q.Enqueue(f.left);
}
if (f.right != null)
{
Q.Enqueue(f.right);
}
}
return floor;
} public IList<int> LargestValues(TreeNode root)
{
var list = new List<int>(); //层次遍历,每一层加入到一个list中,然后再从list中找最大的值
if (root == null)
{
return new List<int>();
}
Q.Enqueue(root);
var floor = new List<TreeNode>();
do
{
floor = FloorSearch();
if (floor.Count > )
{
var max = floor.Max(x => x.val);
list.Add(max);
}
} while (floor.Count > ); return list;
}
}

https://leetcode.com/problems/find-largest-value-in-each-tree-row/#/description

leetcode515的更多相关文章

  1. [Swift]LeetCode515. 在每个树行中找最大值 | Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  2. Leetcode515. Find Largest Value in Each Tree Row在每个树行中找最大值

    您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] class Solution { public: vector<i ...

  3. Python-一些实用的函数

    一,返回值为bool类型的函数 1.any()函数 any(iterable)->bool 当迭代器中有一个是Ture,则返回Ture:若interable=NUll,则返回False. > ...

  4. Python常用函数及说明

    原文地址:博客园  CSDN 基本定制型C.__init__(self[, arg1, ...]) 构造器(带一些可选的参数)C.__new__(self[, arg1, ...]) 构造器(带一些可 ...

  5. LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    515. 在每个树行中找最大值 515. Find Largest Value in Each Tree Row 题目描述 You need to find the largest value in ...

  6. LeetCode通关:连刷三十九道二叉树,刷疯了!

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...

随机推荐

  1. .net collection tips

    1.数组对象都是Array的子类,Array是一个抽象类,不能显示实例化,Array提供了大量操作数组的静态方法 2.ArrayList其实是内部封装了一个array,实现了IList的接口.add ...

  2. hiho1523 数组重排2

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个1-N的排列A1, A2, ... AN,每次操作小Hi可以选择一个数,把它放到数组的最左边. 请计算小Hi最少进 ...

  3. ueditor编辑器【实际项目使用】

    [页面效果] [代码] 说明: ueditor插件的公共代码: 如果是一个可以放在对应模块的js代码中   如果有多个就要提到全局的高度(即提到公共部分) <script type=" ...

  4. java并发--Callable、Future和FutureTask

    在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...

  5. stm32寄存器版学习笔记03 外部中断

    stm32的每个I/O口都可以作为中断输入,要把I/O口设置为外部中断输入,必须将I/O口设置为上拉/下拉输入 或 浮空输入(但浮空的时候外部一定要带上拉或下拉电阻,否则可能导致 中断不停的触发),干 ...

  6. java集成WebSocket向指定用户发送消息

    一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通 ...

  7. RF第二讲--Selenium2Library库的简单实用

    现在对于RF的应用方法已经有很多书介绍了,网上也可以搜到免费的电子书可以学习.今天就简单和大家介绍一下RF的用法之一,就是基于Selenium2Library库的使用. 1.首先需要安装Seleniu ...

  8. @EnableAutoConfiguration注解原理

    前言 Spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷.比如内嵌的tomcat端口默认配置是8080,这些都属于Spring Boot自动配置的范畴,当然其自动配置相当多. s ...

  9. 随机生成国内ip

    function rand_ip(){ $ip_long = array( array('607649792', '608174079'), //36.56.0.0-36.63.255.255 arr ...

  10. nginx+uwsgi 和nginx+gunicorn区别、如何部署

    [线上环境部署Django,nginx+uwsgi 和nginx+gunicorn,这两种方案,应该如何选择?] 大家是采用的何种部署方式? 第一种,高并发稳定一点 我们公司使用的是nginx+gun ...