LeetCode: Find Largest Value in Each Tree Row
2024-09-29 00:47:49
BFS
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> ans = new ArrayList<Integer>();
if (root == null) return ans;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (queue.isEmpty() != true) {
int size = queue.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
max = Math.max(max, node.val);
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
ans.add(max);
}
return ans;
}
}
最新文章
- Google Code Jam 2015 R2 C
- HTTP 错误 500.19- Internal Server Error 错误解决方法
- win10 ctrl + 空格 热键取消
- 【Python实战02】共享Python代码到PyPI社区
- The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.问题解决
- uva 10555 - Dead Fraction)(数论)
- HDU3727--Jewel (主席树 静态区间第k大)
- uvc摄像头代码解析7
- url拼凑示例(具体拼凑规则由服务器定)
- HDOJ2000-ASCII码排序
- Java 获取年 月 日 时 分 秒
- ntp 服务:Server dropped: Strata too high
- element-ui的那些坑与总结
- Windows server 2008 r2下载地址和激活破解方法
- 【leetcode】部分思路整理
- [UE4]控制流
- 获取QQ头像接口
- MVC的项目部署成应用程序或虚拟目录路径的问题
- Python3基础 yield 在函数中的用法示例
- utf-8编码的中文看成2个字符,其他数字字符看成一个字符