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. javascript数组原型方法

    1.javascript数组原型方法. <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  2. 一条命令在Centos7中换163 yum源、安装python3并与python2共存、使用豆瓣pip源加速

    打开一个Terminal: 换yum源: mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup & ...

  3. fastText 安装

    Windows: https://www.lfd.uci.edu/~gohlke/pythonlibs/#fasttext 下载需要的版本 然后:pip install 文件名

  4. 05-python中的异常

    python的所有的异常都继承自基类: Exception 处理方式和java类似: path = raw_input('input the path') array = path.split('/' ...

  5. kendo UI 入门

    Kendo UI 是一套前端开发宽假,意为日本的“剑道” 首先到官方网站下载最新的30天试用版,地址为:http://www.telerik.com/download/kendo-ui 需要简单注册一 ...

  6. 手机浏览器_安卓_苹果手机Webview 中唤醒APP

    Url scheme是iOS,Android平台都支持,只需要原生APP开发时注册scheme, 那么用户点击到此类链接时,会自动跳到APP.比如 <!-- 打开考拉APP首页 --> & ...

  7. 【转】Js获取当前日期时间及其它操作

    Js获取当前日期时间及其它操作 原文地址:http://www.cnblogs.com/carekee/articles/1678041.html var myDate = new Date();my ...

  8. hadoop学习笔记(十):MapReduce工作原理(重点)

    一.MapReduce完整运行流程 解析: 1 在客户端启动一个作业. 2 向JobTracker请求一个Job ID. 3 将运行作业所需要的资源文件复制到HDFS上,包括MapReduce程序打包 ...

  9. [转]How to nest transactions nicely - "begin transaction" vs "save transaction" and SQL Server

    本文转自:http://geekswithblogs.net/bbiales/archive/2012/03/15/how-to-nest-transactions-nicely---quotbegi ...

  10. ASP.NET 之 MVC框架及搭建

    一.MVC简介 MVC:Model-View-Controller(模型-视图-控制器),MVC是一种软件开发架构模式. 1.模型(Model) 模型对象是实现应用程序数据域逻辑的应用程序部件. 通常 ...