题目

https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/

题解

1、参考“二叉树按层打印”写的解法

详细思路参考:左神算法:二叉树的按层打印与ZigZag打印(Java版)

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;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<Double> averageOfLevels(TreeNode root) {ArrayList<Double> list = new ArrayList<>();Queue<TreeNode> queue = new LinkedList<>();TreeNode last = root; // 当前行的最右节点TreeNode nLast = null; // 下一行的最右节点queue.offer(root);double sum = 0;double cnt = 0;while (!queue.isEmpty()) { // 每一层都做从左到右宽度优先遍历root = queue.poll();sum += root.val;cnt++;if (root.left != null) {queue.offer(root.left);nLast = root.left; // 让nLast一直跟踪记录宽度优先队列中的最新加入的节点即可,因为最新加入队列的节点一定是目前已经发现的下一行的最右节点}if (root.right != null) {queue.offer(root.right);nLast = root.right;}if (root == last) { // 如果发现遍历到的节点等于last,则说明应该换行list.add(sum / cnt);sum = 0;cnt = 0;last = nLast; // 当前行累加完时,nLast一定是下一行所有节点中的最右节点,只要令last=nLast,就可以继续下一行的累加过程}}return list;}
}

2、评论区简洁解法

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public List<Double> averageOfLevels(TreeNode root) {List<Double> res = new ArrayList<>();if (root == null) return res;Queue<TreeNode> list = new LinkedList<>();list.add(root);while (list.size() != 0){int len = list.size();double sum = 0;for (int i = 0; i < len; i++){TreeNode node = list.poll();sum += node.val;if (node.left != null) list.add(node.left);if (node.right != null) list.add(node.right);}res.add(sum/len);}return res;}
}

leetcode 637. Average of Levels in Binary Tree | 637. 二叉树的层平均值(Java)相关推荐

  1. Leetcode PHP题解--D56 637. Average of Levels in Binary Tree

    2019独角兽企业重金招聘Python工程师标准>>> D56 637. Average of Levels in Binary Tree 题目链接 637. Average of ...

  2. LeetCode - Easy - 637. Average of Levels in Binary Tree

    Topic Tree Description https://leetcode.com/problems/average-of-levels-in-binary-tree/ Given the roo ...

  3. LeetCode 637. Average of Levels in Binary Tree

    题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o ...

  4. 637. Average of Levels in Binary Tree

    1.问题描述 Given a non-empty binary tree, return the average value of the nodes on each level in the for ...

  5. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  6. Leetcode 742. Closest Leaf in a Binary Tree

    Leetcode 742. Closest Leaf in a Binary Tree Approach #1: Convert to Graph 把树转换为以k节点为中心的图,从k节点开始层序遍历, ...

  7. Java实现 LeetCode 637 二叉树的层平均值(遍历树)

    637. 二叉树的层平均值 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3/ \9 20/ \15 7 输出: [3, 14.5, 11] 解释: 第0层的平均值是 ...

  8. LeetCode: 111. Minimum Depth of Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

  9. leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)

    题目 https://leetcode.com/problems/maximum-width-of-binary-tree/ 题解 本题思路来源于二叉树的层序遍历. 层序遍历类似问题:leetcode ...

最新文章

  1. DataTable ,XML和JSON相互转化
  2. 双剑合璧:邮件客户端与邮件系统
  3. Linux平台安装xtools
  4. java 分贝_java11教程--jhsdb命令
  5. 作者:张志恒(1990-),男,兰州大学资源环境学院硕士生。
  6. mysql 外键详解_mysql 中的外键key值的详解
  7. PDF控件Aspose.Pdf 12月新版17.12发布 | 附下载
  8. Android 微信H5支付,无法拉起微信支付页面
  9. 给视频添加马赛克并裁剪画面的简单步骤
  10. python mp4提取音频,利用python提取视频中的音频
  11. python实现货币的转换_Python实现制度转换(货币,温度,长度)
  12. 微机原理8086CPU结构和功能
  13. 无线网技术——知识点
  14. xshell双击无反应
  15. mysql 生成id函数_MySQL ID生成策略
  16. matlab 端点检测 能零比法_端点检测方法
  17. bp神经网络模式识别,bp神经网络数字识别
  18. 一、elastic-job、elastic-job-lite-console使用案例
  19. 【HSJFramework】Unity时间管理TimeManger计时器
  20. python实现留一法_机器学习基础

热门文章

  1. CodeForces - 1328E Tree Queries(dfs序/LCA)
  2. HDU - 2176 取(m堆)石子游戏(尼姆博奕)
  3. vivaldi浏览器_上网高阶用户怎么能少了这3个无广告、安全免费的浏览器呢
  4. linux 启动程序-p,Linux应用程序开发笔记:配置linuxptp开机启动(ubuntu gPTP)
  5. CAsyncSocket及CSocket的区别和用法
  6. 20.IDA-修改二进制文件、显示修改点
  7. 1.2句柄及 WinMain函数
  8. COM编程之一 组件
  9. SurfaceTexture
  10. MySQL(二)InnoDB的内存结构和特性