/**
* 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. 套接字中的recv与send的注意事项

    recv() 特征* 如果连接的另一端断开连接,则recv立即返回空子串* recv是从接受缓冲区取出内容,当缓冲区为空则阻塞* recv如果一次接受不完缓冲区内容,下次会继续接收 send() 特征 ...

  2. Form表单如何传递List数组对象到后台的解决办法(转)

    举例说明: [后台有一个对象 User    一个PhotoDo对象],结构如下: public class User{ private String username; private List&l ...

  3. SpringMVC和Freemarker整合,带自定义标签的使用方法

    SpringMVC和Freemarker整合,带自定义标签的使用方法. [参考来源:http://www.360doc.com/content/14/1225/14/1007797_435663342 ...

  4. springboot项目启动之后初始化自定义配置类

    前言 今天在写项目的时候,需要再springboot项目启动之后,加载我自定义的配置类的一些方法,百度了之后特此记录下. 正文 方法有两种: 1. 创建自定义类实现 CommandLineRunner ...

  5. 如何快速编写和调试 Emit 生成 IL 的代码

    .NET Core/.NET Framework 的 System.Reflection.Emit 命名空间为我们提供了动态生成 IL 代码的能力.利用这项能力,我们能够在运行时生成一段代码/一个方法 ...

  6. pat甲级 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  7. Python猴子补丁

    属性在运行时的动态替换,叫做猴子补丁(Monkey Patch). 为什么叫猴子补丁 属性的运行时替换和猴子也没什么关系,关于猴子补丁的由来网上查到两种说法: 1,这个词原来为Guerrilla Pa ...

  8. Oracle 之 配置HugePages内存

    HugePages是通过使用大页内存来取代传统的4kb内存页面,使得管理虚拟地址数变少,加快了从虚拟地址到物理地址的映射以及通过摒弃内存页面的换入换出以提高内存的整体性能.尤其是对于8GB以上的内存以 ...

  9. spring容器启动

    1 主要类 ContextLoaderListener:注册在web.xml中,web应用启动时,会创建它,并回调它的initWebApplicationContext()方法,从而创建并启动spri ...

  10. 研究ecmall一些流程、结构笔记 (转)

    index.phpECMall::startup() //ecmall.php object //所有类的基础类 ecmall.phpBaseApp //控制器基础类 app.base.phpECBa ...