当前位置:网站首页>LeetCode 515(C#)
LeetCode 515(C#)
2022-07-07 19:17:00 【Just be interesting】
subject
Given the root node of a binary tree root , Please find the maximum value of each layer in the binary tree .
Example 1:
Input : root = [1,3,2,5,3,null,9]
Output : [1,3,9]
Example 2:
Input : root = [1,2,3]
Output : [1,3]
Code
- 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;
}
}
边栏推荐
- Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products
- App capture of charles+postern
- Cadre de validation des données Apache bval réutilisé
- Numpy——2. Shape of array
- 完整的电商系统
- 反爬虫的重点:识别爬虫
- Short selling, overprinting and stock keeping, Oriental selection actually sold 2.66 million books in Tiktok in one month
- 3.关于cookie
- 超分辨率技术在实时音视频领域的研究与实践
- Tips and tricks of image segmentation summarized from 39 Kabul competitions
猜你喜欢

我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信

SlashData开发者工具榜首等你而定!!!
![Interview vipshop internship testing post, Tiktok internship testing post [true submission]](/img/69/b27255c303150430df467ff3b5cd08.gif)
Interview vipshop internship testing post, Tiktok internship testing post [true submission]

99% of people don't know that privatized deployment is also a permanently free instant messaging software!
![学习open62541 --- [67] 添加自定义Enum并显示名字](/img/98/e5e25af90b3f98c2be11d7d21e5ea6.png)
学习open62541 --- [67] 添加自定义Enum并显示名字

Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry

Policy mode - unity

App capture of charles+drony

GSAP animation library

Command mode - unity
随机推荐
UVALive – 4621 Cav 贪心 + 分析「建议收藏」
How to share the same storage among multiple kubernetes clusters
For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
Seize Jay Chou
In 2021, the national average salary was released. Have you reached the standard?
unity2d的Rigidbody2D的MovePosition函数移动时人物或屏幕抖动问题解决
高温火烧浑不怕,钟薛高想留清白在人间
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
Redis publishing and subscription
SlashData开发者工具榜首等你而定!!!
POJ 2392 Space Elevator
前首富,沉迷种田
99% of people don't know that privatized deployment is also a permanently free instant messaging software!
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
Former richest man, addicted to farming
国内首次!这家中国企业的语言AI实力被公认全球No.2!仅次于谷歌
I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
App capture of charles+postern
L1-019 who falls first (Lua)
PTA 1102 teaching Super Champion volume