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. ...
随机推荐
- hadoop1.x异常
可能是存在class冲突 http://stackoverflow.com/questions/25130799/hadoop-java-io-ioexception-call-to-localhos ...
- ruby:TypeError: 对象不支持此属性或方法(<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolink)
我整理了一下 目前有三种方法,我是windows 7 64位, 答案中很多人是win8也成功了. 方法1:找到当前项目下 app/javascripts/applications.js 把里面的// ...
- 【链表】Swap Nodes in Pairs(三指针)
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- 恶性bug解决,Encoding 1252 data could not be found. Make sure you have correct international codeset assembly installed and enabled
百度是没有的,google了下 这句话的意思是编码1252没找到,确保程序及是国际化格式 发生在我使用unity读取xlsx文件,在编辑器运行正常,但是发布出来不正常,报错 解决方案: 链接:http ...
- 8-lvs-负载均衡
注意: linux集群的时间需要一致 并发量在千万以上, 一般才会使用此种方式, 基于第四层进行ip欺骗, 使得nginx只接受上行流量, 下行流量通过具体执行的服务器直接返回 由章文嵩博士(淘宝) ...
- java的if语句,少于一行可以省略大括号
我们认识的 if 语句,大概是这样的: if(条件){ 语句1; }else{ 语句2; } 如果要执行的语句少于1行,大括号是可以省略的 可以让程序更简洁和美观 if(条件){ 语句1; 语句2; ...
- docker搭建私有registry
搭建docker的私有registry 1. registry简介 Docker在2015年推出了distribution项目,即Docker Registry 2.相比于old registry ...
- nginx学习笔记(8)虚拟主机名---转载
通配符名字正则表达式名字其他类型的名字优化兼容性 虚拟主机名使用server_name指令定义,用于决定由某台虚拟主机来处理请求.具体请参考<nginx如何处理一个请求>.虚拟主机名可以使 ...
- Spring整合Hibernate的XML文件配置,以及web.xml文件配置
利用Spring整合Hibernate时的XML文件配置 applicationContext.xml <?xml version="1.0" encoding=" ...
- Linux内核源码目录
linux和Android的Makefile和android.mk Uboot流程分析(未编辑完) Kernel的IIC驱动分析(未编辑完)