leetCode题解之求二叉树每层的平均值
1、题目描述
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

计算二叉树每一层的节点的数据域的平均值。
2、题目分析
使用广度优先遍历方法,逐层对二叉树进行访问,求均值。使用一个队列数据结构完成对二叉树的访问,队列(queue)的特点是FIFO,即先进先出,queue的几种常用的方法是:
queue::front() :访问队首元素
queue:: pop() 删除队首元素
queue::push() 入队
queue:: back() 访问队尾元素
vector<double> averageOfLevels(TreeNode* root) {
vector<double> ave;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
double temp = 0.0;
int s = q.size();
for( int i = ; i < s;i++ )
{
temp += q.front()->val;
if(q.front()->left) q.push(q.front()->left);
if(q.front()->right ) q.push(q.front()->right);
q.pop();
}
ave.push_back(temp/s);
}
return ave;
}
3、代码
leetCode题解之求二叉树每层的平均值的更多相关文章
- leetCode题解之求二叉树最大深度
1.题目描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along t ...
- 【LeetCode题解】94_二叉树的中序遍历
目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...
- 【LeetCode题解】144_二叉树的前序遍历
目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...
- leetCode题解之反转二叉树
1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...
- Leetcode题解 - 双指针求n数之和
1. 两数之和 """ 双指针,题目需要返回下标,所以记录一个数字对应的下标 """ class Solution: def twoSum( ...
- [LeetCode] 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 of an ...
- LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)
题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
随机推荐
- .gitignore释疑
自己总结: *.a--匹配任意路径下的.a文件 a/--匹配任意路径下的a文件夹 a/*--匹配根目录下的文件夹a下的任意文件(等同于 /a/* 或 /a/) ----一般情况下不会有这种写法 /a ...
- Android4.0 Launcher 源码分析1——Launcher整体结构
1.Launcher整体结构 桌面程序其实并不包含桌面壁纸,桌面壁纸其实是由 WallpaperManagerService来提供,整个桌面其实是叠加在整个桌面壁纸上的另外一个层. 1.1 WorkS ...
- wordpress时间函数the_time() 详解
一直以来,我对时间显示这一块都没有给予些微的关注,但昨天无意间在网络上看到了上面有一篇文章所说的就是the_time()时间函数,感觉很有必要整理一下,便将其中的内容整理了下来.可能很多朋友对于时间的 ...
- javascript 数组去重的6种思路
前端在日常开发中或多或少都会碰到有对数据去重的需求,实际上,像是lodash这些工具库已经有成熟完备的实现,并且可以成熟地运用于生产环境.但是这并不妨碍我们从思维拓展的角度出发,看看去重可以用几种思路 ...
- MySQL查询数据表的Auto_Increment(自增id)
1.一般数据表的id都是设置成auto_increment的,所以当插入一条记录后,可以使用下面的命令来获取最新插入记录的id值 select last_insert_id(); 注意:1. 必须是在 ...
- C#的Equals不区分大小写
代码:System.Windows.Forms.MessageBox.Show("abc".Equals("ABC", StringComparison.Inv ...
- 【设计模式】工厂模式 Factory Pattern
1)简单工厂(不是模式) 简单工厂只是一种变成习惯,并非23种设计模式之一. 简单工厂提供将实例话那种类型留给运行时判断,而非编译时指定.简单工厂模式就是由一个工厂类根据传入的参数决定创建出哪一个类的 ...
- MVC中使用Castle.Windsor
我在MVC中使用Castle.Windsor是这样用的. 首先在UI层安装Install Castle.Windsor 在App_Start中增加一个类WindsorActivator,用于注册和销毁 ...
- IDF实验室-简单的js解密
根据加密方法推算解密方法,补全如下 <script> /** * Pseudo md5 hash function * @param {string} string * @param {s ...
- springboot之约定大约配置
前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样 ...