当前位置:网站首页>LeetCode 515(C#)
LeetCode 515(C#)
2022-07-07 15:38:00 【有趣就行】
题目
给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
示例1:
输入: root = [1,3,2,5,3,null,9]
输出: [1,3,9]
示例2:
输入: root = [1,2,3]
输出: [1,3]
代码
- DFS
public class Solution
{
public IList<int> LargestValues(TreeNode root)
{
List<int> list = new List<int>();
Dfs(list, root, 0);
return list;
}
private void Dfs(IList<int> list, TreeNode node, int depth)
{
if (node == null) return;
if (depth >= list.Count) list.Add(node.val);
else list[depth] = Math.Max(list[depth], node.val);
Dfs(list, node.left, depth + 1);
Dfs(list, node.right, depth + 1);
}
}
- BFS
public class Solution
{
public IList<int> LargestValues(TreeNode root)
{
List<int> list = new List<int>();
Queue<TreeNode> queue = new Queue<TreeNode>();
if (root != null) queue.Enqueue(root);
while (queue.Count > 0)
{
(int sz, int ans) = (queue.Count, int.MinValue);
for (int i = 0; i < sz; i++)
{
var t = queue.Dequeue();
ans = Math.Max(ans, t.val);
if (t.left != null) queue.Enqueue(t.left);
if (t.right != null) queue.Enqueue(t.right);
}
list.Add(ans);
}
return list;
}
}
边栏推荐
- Jenkins发布uniapp开发的H5遇到的问题
- LeetCode 1155. 掷骰子的N种方法 每日一题
- LeetCode 1981. 最小化目标值与所选元素的差 每日一题
- Sator launched Web3 game "satorspace" and launched hoobi
- 国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅
- Blue Bridge Cup final XOR conversion 100 points
- LeetCode 1031. Maximum sum of two non overlapping subarrays
- 管理VDI的几个最佳实践
- Share the latest high-frequency Android interview questions, and take you to explore the Android event distribution mechanism
- Establishment of solid development environment
猜你喜欢

Mrs offline data analysis: process OBS data through Flink job

鲲鹏开发者峰会2022 | 麒麟信安携手鲲鹏共筑计算产业新生态

PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight

What is cloud computing?

skimage学习(1)

Biped robot controlled by Arduino

【图像传感器】相关双采样CDS

Skimage learning (2) -- RGB to grayscale, RGB to HSV, histogram matching
![[Seaborn] combination chart: facetgrid, jointgrid, pairgrid](/img/89/a7cf40fb3a7622cb78ea1b92ffd2fb.png)
[Seaborn] combination chart: facetgrid, jointgrid, pairgrid

Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
随机推荐
Solidity 开发环境搭建
[Fantan] how to design a test platform?
Skimage learning (1)
管理VDI的几个最佳实践
Module VI
国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅
Sator a lancé le jeu web 3 "satorspace" et a lancé huobi
Flash build API Service - generate API documents
蓝桥杯 决赛 异或变换 100分
Mrs offline data analysis: process OBS data through Flink job
Biped robot controlled by Arduino
[fan Tan] after the arrival of Web3.0, where should testers go? (ten predictions and suggestions)
Seaborn数据可视化
Pychart ide Download
QT picture background color pixel processing method
From Devops to mlops: how do it tools evolve to AI tools?
LeetCode 1186. Delete once to get the sub array maximum and daily question
【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
科普达人丨一文弄懂什么是云计算?
[Huang ah code] Why do I suggest you choose go instead of PHP?