题目链接

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. quartz任务调度初次使用记录

    近期公司开发的数据交换系统嵌入了quartz任务调度功能,大概了解了任务调度的整个流程,项目中需要用到它来进行定时任务操作,对数据定时检查以及及时交换. Quartz是OpenSymphony开源组织 ...

  2. blueterm蓝牙超级终端(源码)

    今天FQ访问外文网站砍看见的终端程序,貌似现在已经开源了,这个软件源码方便开发者开发开发蓝牙与相关的设备的通信,所以在此分享下,给需要的人 项目地址:点击打开,感谢作者,我已经实验过了,导入到ecli ...

  3. linux解压与参数介绍

    linux下 各种解压文件使用方法:https://www.jianshu.com/p/ca41f32420d6 解压参数详解:http://www.cnblogs.com/jyaray/archiv ...

  4. JAVA中的多态概念

    多态性是指同一操作作用于某一类对象,可以有不同的解释,产生不同的执行结果. 多态存在的三个必要条件 需要存在继承和实现关系 同样的方法调用而执行不同操作,运行不同代码. 在运行时父类或者接口的引用变量 ...

  5. 【Oracle】曾经的Oracle学习笔记(4-7)多表联合查询,子查询,动态条件查询

    一.多表联合查询 二.子查询 三.动态条件查询 LESSON 4 Displaying Data from Multiple Tables------------------------------- ...

  6. uvm_dpi——DPI在UVM中的实现(一)

    文件: src/dpi/uvm_dpi.svh 类:  无   SystemVerilog DPI,全称SystemVerilog直接编程接口 (英语:SystemVerilog Direct Pro ...

  7. [Git] Create a new repository on the command line

    echo "# xxx" >> README.md git init git add README.md git commit -m "first commi ...

  8. pat甲级1013

    1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highw ...

  9. 【51nod1815】调查任务(Tarjan+拓扑)

    点此看题面 大致题意:有\(N\)个城市由\(M\)条单向道路(图不一定联通),每个城市有一个发达程度\(a[i]\),要求你求出首都\(S\)到城市\(i\)的一条路径上的两个不同城市\(x,y\) ...

  10. opencv使用 findContours

    http://www.jb51.net/article/132217.htm https://www.jianshu.com/p/4bc3349b4611 https://blog.csdn.net/ ...