在每个树行中找最大值

给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> largestValues(TreeNode root) {if(root == null){return new ArrayList();}List<Integer> ans = new ArrayList();Queue<TreeNode> queue = new LinkedList();Queue<TreeNode> nextLevel = new LinkedList();int maxVal = root.val;TreeNode cur = root;queue.offer(cur);while(!queue.isEmpty()){cur = queue.poll();maxVal = maxVal > cur.val ? maxVal : cur.val;if(cur.left!=null){nextLevel.offer(cur.left);}if(cur.right!=null){nextLevel.offer(cur.right);}if(queue.isEmpty()){ans.add(maxVal);if(!nextLevel.isEmpty()){TreeNode tmp = nextLevel.peek();maxVal = tmp.val;}queue = nextLevel;nextLevel = new LinkedList();}}return ans;}
}

执行用时比较慢, 优化层序遍历只使用一个队列, 利用队列长度控制每层遍历

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> result = new LinkedList<>();if (root == null) {return result;}Queue<TreeNode> q = new LinkedList<>();q.offer(root);// 层序遍历while (!q.isEmpty()) {int sz = q.size();// 在每一层维护一个变量,用于存储最大值int max = Integer.MIN_VALUE;// 对每一层的元素进行遍历for (int i = 0; i < sz; i++) {TreeNode cur = q.poll();// 每取出来一个元素就进行一次比较max = Math.max(max, cur.val);// 添加下一层的元素if (cur.left != null) {q.offer(cur.left);}if (cur.right != null) {q.offer(cur.right);}}// 将每一层的最大值添加到结果变量中result.add(max);}return result;}
}

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

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

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

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

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

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

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

  4. 515. 在每个树行中找最大值

    您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1/ \3 2/ \ \ 5 3 9 输出: [1, 3, 9] 在真实的面试中遇到过这道题? class Solution {public L ...

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

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

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

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

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

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

  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. Connot resolve Symbol '.......'
  2. [PKUWC2018]随机算法
  3. Hessian源码分析(java)
  4. AI之最——地平线创始人兼CEO余凯谈AI
  5. Educational Codeforces Round 2 B. Queries about less or equal elements
  6. 【错误记录】Android 中使用 Kotlin 为 EditText 组件设置文本报错 ( Type mismatch. Required:Editable. Found:String )
  7. 关于C/S模式开发的学习笔记
  8. mysql update delete_MySQL中UPDATE与DELETE语句的使用教程
  9. Android CertificateSource系统根证书的检索和获取
  10. 阿尔法平台C语言编程题答案
  11. 阿古斯机器人_炉石传说三选一卡池全部卡牌一览 挑战之旅冒险模式攻略
  12. c语言按一个按钮弹出窗口,实现点击按钮,弹出输入框的内容
  13. Java新特性(一 · JDK1.5)
  14. 《东周列国志》第十二回 卫宣公筑台纳媳 高渠弥乘间易君
  15. 计算机的关闭程序,电脑中取消关机时强制关闭程序提醒的方法
  16. pytorch入门强化教程——数据加载和处理
  17. Vue中使用SVG-ICON
  18. vue 首页白屏优化
  19. coffeescript语法入门
  20. switchresx卸载_SwitchResX

热门文章

  1. VR教程来了!谷歌设计师出品的VR设计入门指南
  2. 【深入kotlin】 - 协程的取消
  3. 用小姐姐的声音为你指路,实时语音导航功能如何实现?
  4. GEE行政区加载,高程坡度计算和裁剪
  5. Kubernetes中配置livenessProbe、readinessProbe和startupProbe
  6. 手工测试点点点,测着测着,找前台小姐姐看电影去了···
  7. 计算机图形学:二维图形的几何变换(算法原理及代码实现)
  8. 内存容量大小单位转换
  9. SAP英语专栏:第1篇
  10. c语言程序设计 学院工资管理系统,学院教职工工资管理系统C语言程序设计.pdf...