题目链接

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

题目描述

您需要在二叉树的每一行中找到最大的值。

示例:

输入: 

          1
/ \
3 2
/ \ \
5 3 9 输出: [1, 3, 9]

题解

按层次遍历二叉树,计算每层的最大值即可。

代码


/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) { return list; }
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root); while (!queue.isEmpty()) {
int size = queue.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (node.val > max) { max = node.val; }
if (node.left != null) { queue.offer(node.left); }
if (node.right != null) { queue.offer(node.right); }
}
list.add(max);
}
return list;
}
}

Leetcode 515. 在每个树行中找最大值的更多相关文章

  1. 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 ...

  2. Java实现 LeetCode 515 在每个树行中找最大值

    515. 在每个树行中找最大值 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] /** * Definition for ...

  3. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

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

    在二叉树的每一行中找到最大的值.示例:输入:           1         /  \        3   2       /  \    \        5   3    9 输出: [ ...

  5. [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 ...

  6. C语言递归之在每个树行中找最大值

    题目描述 您需要在二叉树的每一行中找到最大的值. 示例 输入: / \ / \ \ 输出: [, , ] 题目要求 /** * Definition for a binary tree node. * ...

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

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

  8. [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

  9. LeetCode刷题总结-树篇(中)

    本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...

随机推荐

  1. 文本编辑简体中文专业版EmEditor Professional v12.0.8(12/27/2012更新)姓名+注册码

    这是一个简单好用的文本编辑器,支持多种配置,自定义颜色.字体.工具栏.快捷键设置,可以调整行距,避免中文排列过于紧密,具有选择文本列块的功能(按ALT 键拖动鼠标),并允许无限撤消.重做,总之功能多多 ...

  2. VS2017无法进入断点调试且移动到breakpoint上的时候报错“breakpoint will not currently be hit. the source code is different from original version. ”

    我尝试了网上的很多其他办法也翻阅了很多外网资源,这些方法并不能解决我的问题 当然我非常震惊正当我尝试着在stack overflow上发表评论交流一下究竟如何解决的时候,却发现有方法灵验了 ,但是每个 ...

  3. Photoshop之切图

    基本(繁琐)操作: 切JPG图(即带背景的图): 1.         选切片工具(另外,切片选择工具能选择切片和删除切片),切 2.         存储为Web所用格式(快捷键Ctrl + Shi ...

  4. oracle 查询之前的表数据

    SELECT * FROM Student  AS OF TIMESTAMP SYSDATE - 3/1440 对SQL的解释说明: SYSDATE :当前时间 1440 :24h*60m=1440m ...

  5. slfj4 + logback

    slf4j:(Simple Logging Facade for Java,简单日志门面),它不是具体的日志解决方案,只服务于各种各样的日志系统.在使用SLF4J的时候,不需要再代码中或配置文件中指定 ...

  6. .net密码找回

    using System; using System.Collections.Generic; using System.Text; using System.Net.Mail; using Syst ...

  7. adc verilog spi 时序

    我用的是adc081sd芯片,(由于我们使用的是FPGA不用像单片机那样考虑极性cpol,相位cpha,下面仅仅介绍下跟单片机比较下) 什么是cpol:若cs被拉为低电平时sclk(时钟)是高那么cp ...

  8. JS中的Global对象

    Global对象可以说是ECMAScript中最特别的一个对象了.因为不管你从什么角度上看,这个对象都是不存在的.ECMAScript中的Global对象在某种意义上是作为一个终极的“兜底儿对象”来定 ...

  9. UVALive 4731 Cellular Network(贪心,dp)

    分析: 状态是一些有序的集合,这些集合互不相交,并集为所有区域.显然枚举集合元素是哪些是无法承受的, 写出期望的计算式,会发现,当每个集合的大小确定了以后,概率大的优先访问是最优的. 因此先对u从大到 ...

  10. 3218: 字符串字符统计—C语言

    3218: 字符串字符统计—C语言 时间限制: 1 Sec  内存限制: 128 MB提交: 270  解决: 129[提交][状态][讨论版][命题人:smallgyy] 题目描述 编写一函数,由实 ...