您需要在二叉树的每一行中找到最大的值。

示例:

输入: 1/ \3   2/ \   \  5   3   9 输出: [1, 3, 9]

在真实的面试中遇到过这道题?
class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> res = new ArrayList<>();Queue<TreeNode> queue = new LinkedList<>();if(root == null) return res;queue.add(root);while(!queue.isEmpty()){int size = queue.size();int Max = Integer.MIN_VALUE;for(int i=0; i<size; i++){TreeNode node = queue.poll();if(node.val > Max) Max = node.val;if(node.left != null) queue.add(node.left);if(node.right != null) queue.add(node.right);}res.add(Max);}return res;}
}

转载于:https://www.cnblogs.com/Roni-i/p/10510237.html

515. 在每个树行中找最大值相关推荐

  1. LeetCode二叉树系列——515.最每个树行中找最大值

    一.题目描述: 515. 在每个树行中找最大值 难度中等265收藏分享切换为英文接收动态反馈 给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值. 二.题解 对二叉树不了解的,可以先看 ...

  2. leetcode:515. 在每个树行中找最大值

    题目来源 515. 在每个树行中找最大值 题目描述 题目解析 层次遍历 /*** Definition for a binary tree node.* public class TreeNode { ...

  3. 【二叉树】515. 在每个树行中找最大值

    在每个树行中找最大值 给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值. /*** Definition for a binary tree node.* public class ...

  4. leetcode系列-515.在每个树行中找最大值

    题目描述: 给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值. 示例1: 输入: root = [1,3,2,5,3,null,9] 输出: [1,3,9]示例2: 输入: root ...

  5. leetcode 515. 在每个树行中找最大值(层序遍历06)

    一:题目 二:上码 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* Tre ...

  6. LeetCode 515. 在每个树行中找最大值(层序遍历)

    1. 题目 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1/ \3 2/ \ \ 5 3 9 输出: [1, 3, 9] 来源:力扣(LeetCode) 链接:https://leetco ...

  7. LeetCode 515. 在每个树行中找最大值(广度优先搜索)

    题目描述 您需要在二叉树的每一行中找到最大的值. 思路 详见链接 代码 class TreeNode:def __init__(self,x):self.val = xself.left = None ...

  8. 深度优先遍历和广度优先遍历_利用广度优先搜索解LeetCode第515题:在每个树行中找最大值...

    题目描述(难度中等) 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1/ 3 2/ 5 3 9 输出: [1, 3, 9] 解题思路 利用广度优先搜索找到每一层的所有数字,将其中最大的数字存 ...

  9. 【LeetCode-515 | 在每个树行中寻找最大值】

    1.题目描述 2.题解 #include <iostream> #include <vector> #include <queue> #include <st ...

最新文章

  1. Typescript在Vue项目中的使用
  2. 全球10大SSL证书品牌对比
  3. 通过VisualSVN的POST-COMMIT钩子自动部署代码
  4. structs2之多文件上传
  5. Fastjson - 详解SerializeFilter,格式化对象字段
  6. error LNK2001: 无法解析的外部符号 _IID_IDirectDraw7
  7. sql server 多用户 并发_Tinyid:滴滴开源千万级并发的分布式ID生成器
  8. SQL Server中的版本号
  9. UDP socket--bind
  10. 微pe怎么装linux系统,微PE工具箱增加安装Linux系统菜单
  11. Python+Matplotlib科研绘图
  12. MasterCAM快捷功能大全
  13. 【微信小程序】微信公众平台合法域名设置
  14. MAC——本机域名[localhost]配置
  15. Oracle配置本地网络服务器测试不成功,无监听程序
  16. 吴军老师的《计算之魂》部分重点摘要
  17. 增量式编码器和绝对式编码器
  18. Ubuntu18.04通过apt安装libssl1.1因依赖问题导致失败的解决办法
  19. 图像特征(二)——形状特征(主轮廓特征、区域特征、图像的矩及Hu矩)
  20. System.Diagnostics.Process.Start的妙用

热门文章

  1. 线程池的种类,区别和使用场景
  2. WebBrowser控件禁用超链接转向、脚本错误提示、默认右键菜单和快捷键
  3. node --- Missing write access to 解决
  4. Spring Cloud Gateway 路由转发之After(Before)路由断言工厂使用
  5. Linux 操作命令 more
  6. Palo Alto Networks全球化安全堡垒理念 提升企业防御能力
  7. 自然语言交流系统 phxnet团队 创新实训 项目博客 (五)
  8. Swift 开发的工具类,主要是提供正则表达式及其它,Github会长期维护
  9. 【活动】畅想云端加油站,赢iPad
  10. SQL SERVER 中identity