LeetCode: Find Largest Value in Each Tree Row
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;
}
}
LeetCode: Find Largest Value in Each Tree Row的更多相关文章
- [LeetCode] 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 ...
- LeetCode——Find Largest Value in Each Tree Row
Question You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- 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 ...
- LN : leetcode 515 Find Largest Value in Each Tree Row
lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...
- 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- leetcode算法: 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 ...
- (BFS 二叉树) leetcode 515. 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 ...
- 【leetcode】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 ...
随机推荐
- 安装Node.js以及Hexo
安装前提 安装 Hexo 相当简单.然而在安装前,您必须检查电脑中是否已安装下列应用程序: Node.js 如何在Ubuntu上安装最新版本的Node.js https://hexo.io/zh-cn ...
- 【BZOJ】1661: [Usaco2006 Nov]Big Square 巨大正方形(暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=1661 暴力大法好... 枚举对角线(注意,一种对角线2种情况就行了,自己想...) 然后可以算出其它 ...
- 卡友pos机使用流程
Q: pos机正常使用步骤 A: 1. 按开机键开机2. 输入“01”进行签到3. 系统提示输入密码,密码为“0000”4. 系统提示“请刷卡”,可正常刷卡消费首次使用请务必登陆商户后台核对结算收款账 ...
- jdk从1.8降到1.7的办法
jdk从1.8降到1.7的办法 正常情况下,JDK的升降级只需更改环境变量即可,可是到了1.8版本,oracle使了一点小手段--在系统Path中添加一条变量C:/ProgramData/Oracle ...
- Ubuntu14.4下安装FTP
今天学习了Ubuntu上安装ftp的实例. 1.window上已经安装wamp(windows + apache + mysql + php) 2.window上已安装VMware Workstati ...
- 详略。。设计模式2——单例变形(多例).。。。studying
★ 缓存在单例中的使用("单例+缓存"技术) 缓存在编程中使用非常频繁,有着非常关键的数据,它可以帮助程序实现以空间换取时间, 通常被设计成整个应用程序所共享的一个空间,现要求实现 ...
- VC++显示文件或文件夹属性
When you select a file or folder in Explorer window, and choose 'Properties' from the menu, you get ...
- Log4j 汇总
一.概念 .1. log4j是 是线程安全的 日志框架,高度可配置,可通过在运行时的外部文件配置. 默认情况下,日志管理在CLASSPATH 查找一个名为 log4j.properties 的文件. ...
- 【转载】如何升级linux上的gcc到最新版本
来自:http://www.cppfans.org/1719.html 由于工作主要平台换到了linux上,而linux因为源上没有比较新的gcc,只有4.7,而我们用到了C++11, 只好自己升级了 ...
- Laravel5.1 模型 --远层一对多关系
远层一对多我们可以通过一个例子来充分的了解它: 每一篇文章都肯定有并且只有一个发布者 发布者可以有多篇文章,这是一个一对多的关系.一个发布者可以来自于一个国家 但是一个国家可以有多个发布者,这又是一个 ...