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. sql 联合查询

    SELECT NZD_Products.Prd_typename a FROM  dbo.NZD_Products where dbo.NZD_Products.Prd_barcodenumber = ...

  2. AWT是Java基础类 (JFC)的一部分,为Java程序提供图形用户界面(GUI)的标准API

    抽象窗口工具包 (Abstract Windowing Toolkit) (AWT)是Java的平台独立的窗口系统,图形和用户界面器件工具包. AWT是Java基础类 (JFC)的一部分,为Java程 ...

  3. python Virtual Environments

    Install $ pip install virtualenv Basic usage 在一个项目中创建一个虚拟环境 $ cd my_project_folder $ virtualenv venv ...

  4. 正确的关机方法: sync, shutdown, reboot, halt, poweroff, init

    正常情况下,要关机时需要注意底下几件事: 观察系统的使用状态: 如果要看目前有谁在在线,可以下达『who』这个命令,而如果要看网络的联机状态,可以下达 『 netstat -a 』这个命令, 而要看背 ...

  5. C语言程序设计-猴子选大王[链表应用]

    2032 猴子选大王 Description 有N只猴子,从1~N进行编号.它们按照编号的顺时针方向排成一个圆圈,然后从第一只猴子开始报数.第一只猴子报的第一个数字为1,以后每只猴子报的数字都是它们前 ...

  6. 一个Demo展示Storyboard的强大

    本文转载至http://www.cocoachina.com/ios/20150330/11440.html 今天我通过完成一个长按cell删除的Demo,向你们展示熟练运用storyboard和Au ...

  7. Delphi MessageBox

    MessageBox对话框是比较常用的一个信息对话框,其不仅能够定义显示的信息内容.信息提示图标,而且可以定义按钮组合及对话框的标题,是一个功能齐全的信息对话框信息提示图标,而且可以定义按钮组合及对话 ...

  8. linux系统中利用vagrant创建虚拟开发环境

    Vagrant简介 作为程序员,可能需要同时开发多个项目,使用多种编程语言,需要使用各种操作系统,如果将很多东西放在同一个电脑上,肯定会被各种配置环境搞晕.一个比较好的办法就是每个项目都有一个干净的开 ...

  9. bootstrap Table API和一些简单使用方法

    官网: http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/ 后端分页问题:后端返回”rows”.“”total,这样才能重新赋值 ...

  10. delphi =------报错

    关于delphi软件运行出现Invalid floating point operation的错误的解决办法 关于delphi软件运行出现Invalid floating point operatio ...