Leetcode 515. 在每个树行中找最大值
题目链接
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. 在每个树行中找最大值的更多相关文章
- 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 ...
- Java实现 LeetCode 515 在每个树行中找最大值
515. 在每个树行中找最大值 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] /** * Definition for ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- 515 Find Largest Value in Each Tree Row 在每个树行中找最大值
在二叉树的每一行中找到最大的值.示例:输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [ ...
- [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 ...
- C语言递归之在每个树行中找最大值
题目描述 您需要在二叉树的每一行中找到最大的值. 示例 输入: / \ / \ \ 输出: [, , ] 题目要求 /** * Definition for a binary tree node. * ...
- Leetcode515. Find Largest Value in Each Tree Row在每个树行中找最大值
您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] class Solution { public: vector<i ...
- [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 ...
- LeetCode刷题总结-树篇(中)
本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...
随机推荐
- 项目上传至Github
到https://github.com/ 注册用户,然后点 Start a project,创建仓库 记住这个 地址. 再去 https://git-scm.com/downloads 下载git 安 ...
- 比例缩放 on() prop() 正则表达式
$('#banner-section').css('height',$(window).width() / 1900 * 490 ); $(window).resize(function(){ ...
- IIS 处理高并发
1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Settings Queu ...
- .NET 前台调用后台事件和方法实现小结
转自:https://www.cnblogs.com/kinger906/p/3431842.html 除了下文讲的方式外,还有一种方式:html里面使用ajax写好提交方式和提交参数,然后以写一行带 ...
- WebAPI项目添加定时服务
开发平台: VS2019 背景: 在开发小程序的API服务的时候,由于access_token的有效期为7200秒,也就是2小时,这就需要后端定时的去更新这个access_token,便于调用小程序的 ...
- pat乙级1045
从左到右扫描时记录扫描到当前下标为止的最大值,如果当前元素大于这个最大值,那么它就大于它左边的所有值.同理,从右到左扫描记录扫描到当前下标为止的最小值,如果当前元素小于这个最大小值,那么它就小于它右边 ...
- app再次进入数据不加载问题
问题原因:触发点击事件在加载页面之前完成. 1.调整了一下页面加载顺序 2.增加了settime的时间
- echarts 相关属性介绍
title: {//图表标题 x: 'left', //组件离容器左侧的距离,left的值可以是像20,这样的具体像素值, 可以是像 '20%' 这样相对于容器高宽的百分比,也可以是 'lef ...
- Redis安装配置及在Python上的应用
最近在使用Kazoo(开源电话系统) API时,一次请求的处理需要调用几次API,只为了得到一个name和id的对应关系,耗时非常大,开始想使用一种简单的实现,直接将对应关系保存到静态类的静态变量中, ...
- Aizu 2301 Sleeping Time(概率,剪枝)
根据概率公式dfs即可,判断和区间[T-E,T+E]是否有交,控制层数. #include<bits/stdc++.h> using namespace std; int K,R,L; d ...