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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  4. 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 ...

  5. 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 ...

  6. 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  7. 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 ...

  8. (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 ...

  9. 【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 ...

随机推荐

  1. 为 Ruby 程序员准备的 Go 入门教程

    这是我翻译的国外博客,如需转载请注明出处和原文链接 那些在Google的大牛们开发出了一种称为Go的牛叉的语言.乍一看,Ruby和Go有点像远房表亲.其实不然,他们那些互为补充的功能却让他们成为一对完 ...

  2. Deep3d研究

    如何使用CNN将视频从2D到3D进行自动转换 http://www.sohu.com/a/128924237_642762 从2D图片生成3D模型(3D-GAN) http://blog.topspe ...

  3. 第一百六十二节,jQuery入门介绍

    jQuery入门 学习要点: 1.什么是  jQuery 2.学习 jQuery的条件 3.jQuery的版本 4.jQuery的功能和优势 5.其他 JavaScript库 6.是否兼容低版本  I ...

  4. Timer类与TimerTask类

    有个schedule方法,可以指定过多长时间定期的执行某个程序或某段代码,或者过多长时间启动一个线程等. TimerTask类实现了Runnable接口,要执行的类由它里面实现的run方法来完成. 编 ...

  5. c# 将html添加进剪贴板(带格式)

    调用: ClipboardHelper.CopyToClipboard("<h1>hello world</h1>", ""); /// ...

  6. point-position目标定位

    双站探测同一目标会构成两条直线:(飞行目标定位2 - ostartech - 博客园 https://www.cnblogs.com/wxl845235800/p/8858116.html) 测角偏差 ...

  7. 上传文件ie7

    https://www.cnblogs.com/front-end-develop/p/6214818.html 第一步:html中引入jQuery-1.7.1.js和ajaxFileUpload.j ...

  8. 提高SDN控制器拓扑发现性能

    原文由我发表在sdnlab.com.原文链接:http://www.sdnlab.com/15425.html SDN网络的一大特点就是资源由控制器集中管理,控制器管理网络,最基本的当然需要知道网络的 ...

  9. adb 连接小米1S真机调试

    Ubuntu13.04 adb连接小米1S真机调试 搭好Android开发环境后,新建了一个Android Application工程.准备运行,问题来了,模拟器太慢了,怎么在真机上调试呢?百度之,G ...

  10. 处理界面上使用两个jq的报错

    转载:http://www.365mini.com/page/jquery_noconflict.htm <script src="jquery-1.9.1.js">& ...