1.问题描述

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Example 1:Input:3/ \9  20/  \15   7Output: [3, 14.5, 11]Explanation:The average value of nodes on level 0 is 3,  on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].Note:1. The range of node's value is in the range of 32-bit signed integer.来自 <https://leetcode.com/problems/average-of-levels-in-binary-tree/description/>

2.题目分析

求二叉树每一层的平均数,并存入到数组中,遍历方法使用层序遍历,关键是怎么判断一层结束了,区别于层序遍历的单队列,这里使用了两个队列,一个存储父节点层,另一个存储子节点层,再父节点层遍历完成后,计算均值并存储,然后交换这两个队列,使得当前子节点层成为下一次计算的父节点层。结束的条件是两个队列都空了。需要注意的是,当父结点层存在,子结点层不存在,即最后一层时,并没有计算最后一层均值,因此需要在循环结束时,计算最后一层均值并存储。

3.C++代码

   //我的代码:(beats 42%)vector<double> averageOfLevels(TreeNode* p){vector<double>r;queue<TreeNode*>q_root;queue<TreeNode*>q_child;q_root.push(p);int cnt = 0;double sum = 0;double aver = 0;while (!q_root.empty() || !q_child.empty()){if (!q_root.empty()){TreeNode *tmp = q_root.front();q_root.pop();if (tmp->left != NULL)q_child.push(tmp->left);if (tmp->right != NULL)q_child.push(tmp->right);cnt++;sum += tmp->val;}else{aver = sum / cnt;cnt = 0;sum = 0;q_root.swap(q_child);r.push_back(aver);}}aver = sum / cnt;r.push_back(aver);return r;}//改进版:(beats 74%)//只用一个队列,每次大循环处理一行,而不是只处理一个结点//利用每一层的结点个数就是队列的长度这一特点,减少一个队列vector<double> averageOfLevels2(TreeNode* p){vector<double>r;queue<TreeNode*>q;q.push(p);int n = 0;double sum = 0;double aver = 0;while (!q.empty()){n = q.size();sum = 0;for (int i = 0; i < n; i++){TreeNode*tmp = q.front();q.pop();sum += tmp->val;if (tmp->left)q.push(tmp->left);if (tmp->right)q.push(tmp->right);}aver = sum / n;r.push_back(aver);}return r;}

637. Average of Levels in Binary Tree相关推荐

  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. leetcode 637. Average of Levels in Binary Tree | 637. 二叉树的层平均值(Java)

    题目 https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/ 题解 1.参考"二叉树按层打印"写的解法 ...

  5. leetcode 199. Binary Tree Right Side View | 199. 二叉树的右视图(Java)

    题目 https://leetcode-cn.com/problems/binary-tree-right-side-view/ 题解 本题思路来源于二叉树的层序遍历. 层序遍历类似问题:leetco ...

  6. 由任意二叉树的前序遍历序列和中序遍历序列求二叉树的思想方法_算法与数据结构基础 - 二叉树(Binary Tree)...

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

  7. LeetCode 662. Maximum Width of Binary Tree

    原题链接在这里:https://leetcode.com/problems/maximum-width-of-binary-tree/ 题目: Given a binary tree, write a ...

  8. LintCode - Ladder3 Binary Tree Divide Conquer

    Oct 14th Tree 结构是否会比其它结构更难建立? no!! 创建的时间都是一样的,每个元素都添加一遍 一定是O(n) 的时间复杂度 Before the Start Basic Knowle ...

  9. LeetCode 102. Binary Tree Level Order Traversal--递归,迭代-Python,Java解法

    题目地址: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

最新文章

  1. 7、Spring -Cloud-路由网管Spring Cloud Zuul
  2. IOS UIView 放大缩小
  3. 倒序存放数组java_java实现数组中的逆序对
  4. mac USB串口工具配置
  5. Java的子类可以继承父类的静态变量和静态方法吗?
  6. Linux问题分析或解决_samba无法连接
  7. ESFramework介绍之(23)―― AgileTcp
  8. 从Java程序员进阶到架构师,6大核心技能要领详解
  9. linux命令系列-zip(压缩打包)
  10. Linux下svn 安装搭建配置流程
  11. python可以神奇的做什么_可以用 Python 编程语言做哪些神奇好玩的事情?
  12. 普通摄像头的数据输出格式YUV与mjpeg之间联系、DCT离散余弦变换去噪跟压缩(待补充)
  13. 20.6. TeXstudio(LaTeX 编辑器)
  14. 美团外卖订单系统演进
  15. 145分计算机考研408复习复盘
  16. 大学生安卓期末设计之本地音乐播放器
  17. 数据交易,距离生产要素市场化还有多远? | 2022全球数字价值峰会
  18. 笔记本电脑如何用c语言开无线网卡,教你如何用无线网卡做wifi热点(适用笔记本)...
  19. oracle中..符号,oracle中带有特殊符号的模糊查询
  20. 琵琶行·并序--白居易

热门文章

  1. 22家安卓应用商店名单
  2. java人民币大小写转换_人民币大小写转换 java 实现
  3. 据国外媒体Theverge称微软目前正计划整合Windows Phone应用商店
  4. Android apps浅析01-Amazed:一个简单但令人上瘾的加速度为基础的大理石指导游戏。...
  5. 三翼鸟,从做家务开始打破家居生活的“标准范式”
  6. 领域驱动设计在美团点评业务系统的实践
  7. 时尚圈元宇宙,时尚圈和元宇宙的碰撞会带来怎样的火花?时尚圈元宇宙,时尚圈和元宇宙的碰撞会带来怎样的火花?
  8. 27计算机表演赛命题,(历史2)27届计算机表演赛命题搜索赛答案
  9. 高等数学(一)夹逼定理
  10. Leetcode刷题——剑指offer_1