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题解之求二叉树每层的平均值的更多相关文章

  1. leetCode题解之求二叉树最大深度

    1.题目描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along t ...

  2. 【LeetCode题解】94_二叉树的中序遍历

    目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...

  3. 【LeetCode题解】144_二叉树的前序遍历

    目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...

  4. leetCode题解之反转二叉树

    1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...

  5. Leetcode题解 - 双指针求n数之和

    1. 两数之和 """ 双指针,题目需要返回下标,所以记录一个数字对应的下标 """ class Solution: def twoSum( ...

  6. [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 ...

  7. 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 ...

  8. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  9. [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. ...

随机推荐

  1. Django中涉及金融的项目

    在Django中,如果一个项目涉及了金融,他的要求是十分严格的. 所以嘞,这里就有一些坑,很多坑,第一次开发的时候很容易出现一系列的错误 在涉及金融计算的地方,不能使用float类型 什么鬼,但事实就 ...

  2. javascript对象属性的命名规则

    JS标识符的命名规则,即变量的命名规则: 标识符只能由字母.数字.下划线和‘$’组成 数字不可以作为标识符的首字符 对象属性的命名规则 通过[]操作符为对象添加属性时,属性名称可以是任何字符串(包括只 ...

  3. centos6.7 安装 virtualBox 再安装 centos 7

    Tag: 黄色为自己实际情况需要设置的部分,绿色部分为虚拟机名称(自定义) 1.创建虚拟机VBoxManage createvm --name centos7 --ostype Linux26_64 ...

  4. NoSQL之Cassandra

    http://www.cnblogs.com/LBSer/p/3328841.html 9月初听了一个讲座,演讲者是张月同学,他给我们分享了Cassandra nosql数据库,讲得很精彩,听完之后收 ...

  5. GDI+中发生一般性错误的解决办法(转)

    今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现"GDI+中发生一般性错误" ...

  6. Spark of work

    Today I attended a meeting of reviewing code,  and I learned a lot from it. In the discuss, we found ...

  7. Android - 单例模式线程安全

    https://blog.csdn.net/Mars_idea/article/details/80724404 https://blog.csdn.net/cselmu9/article/detai ...

  8. 有趣:256个class选择器可以干掉1个id选择器——张鑫旭

    我们应该都知道,从选择器得分权重上将,id选择器(#aaa{})和class选择器(.aaa{})完全不是一个数量级的,前者:1-0-0; 而后者为0-1-0.因此: #id { color:dark ...

  9. 思维导图(JavaScript基础)——温习一下下

  10. [js常用]将秒转化为时分秒

    内容引入至网络 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...