题目链接

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. Sql Server 2012 存储过程的单步调试

    最近在做vb项目的时候,用到了存储过程的调试,现在总结一下发现单步调试存储过程有以下2种方法: 1.这种方法自己已经做过,是可以的,如下: a.如果目标数据库存在存储过程,右击该存储过程-修改,打开存 ...

  2. spring-boot整合shiro作权限认证

    spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro 废话不多说  引入pom文件 <!--shiro集成spring--& ...

  3. BZOJ3624: [Apio2008]免费道路(最小生成树)

    题意 题目链接 Sol 首先答案一定是一棵树 这棵树上有一些0边是必须要选的,我们先把他们找出来,如果数量$\geqslant k$显然无解 再考虑继续往里面加0的边,判断能否加到k条即可 具体做法是 ...

  4. Eucalyptus(v4.0)系统需求

    1.计算需求 Physical Machines: All Eucalyptus components must be installed on physical machines, not virt ...

  5. 【Android开发笔记】杂项

    Android studio shift+enter : start new line Theme 将     <style name="AppBaseTheme" pare ...

  6. 常用宏OC

    #ifndef MacroDefinition_h #define MacroDefinition_h //-------------------获取设备大小--------------------- ...

  7. DBMS事务的四大特性

    数据库事务的四大特性分别是:原子性.一致性.隔离性和持久性. 特性 说明 实现 一致性 在一个事务执行之前和执行之后数据库都必须处于一致性状态.假如数据库的状态满足所有的完整性约束,也可以说数据库是一 ...

  8. C,通信,多线程,数据库小练习——在线电子词典第一版

    #ifndef FILE_H #define FILE_H #include<stdio.h> #include<sys/types.h> #include<sys/so ...

  9. python实现栈的算法

    以下来源“悟空”的讲课视频,我只是对内容加深以下理解,做一些说明: 栈作为一种数据结构,是一种只能在一端进行插入和删除操作.它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要 ...

  10. Unix系统中常用的信号含义

    http://blog.csdn.net/u012349696/article/details/50687462 编号为1 ~ 31的信号为传统UNIX支持的信号,是不可靠信号(非实时的),编号为32 ...