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. jquery mobile demo

    <!DOCTYPE html> <html> <head> <title>jQuery Mobile Demo</title> <me ...

  2. tp三级联动

    <script type="text/javascript">$(document).ready(function(){  $("#province" ...

  3. PHP urlencode

    url get传参时,对参数值需要用urlencode()处理,防止参数中含有特殊字符&等 例如: 一产品名称为A&T Plastic,在产品列表中就产生了这样的一个联接<a h ...

  4. linux的bash与sh的区别

    转自:https://zhidao.baidu.com/question/305415121.html https://zhidao.baidu.com/question/176780008.html ...

  5. 关于小程序报错 缺少文件,错误信息:error: iconPath=../images/home.png, file not found

    事实上在小程序中,虽然你的image文件夹是和你index文件夹的父级文件夹并行的文件夹,但是你如果引用的时候,不用去遵循    ../   或者   ./因为在小程序当中他根本不识别.所以要引用的话 ...

  6. QQ视频直播架构及原理 流畅与低延迟之间做平衡 音画如何做同步?

    QQ视频直播架构及原理 - tianyu的专栏 - CSDN博客 https://blog.csdn.net/wishfly/article/details/53035342 作者:王宇(腾讯音视频高 ...

  7. PHP去除所有的空格

    1.去除两边的空格 trim($arr) 2.正则匹配去除所有的空格 preg_replace('# #','',$goodid)

  8. Linux network 资料链接

    1.iptables 基础 https://wiki.centos.org/HowTos/Network/IPTables 2.HOWTOs on netfilter site http://www. ...

  9. django 多并发,多线程。

    参考http://blog.csdn.net/u013378306/article/details/76215982 django 原生为单线程序,当第一个请求没有完成时,第二个请求辉阻塞,知道第一个 ...

  10. json的相关操作

    最近对json的操作不是很理解 定义: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 它基于 ECMAScript (w3c制定的j ...