给定一个非空二叉树, 返回一个由每层节点平均值组成的数组.

class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> res;
if(root == NULL)
return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
int len = q.size();
double sum = 0;
for(int i = 0; i < len; i++)
{
TreeNode *node = q.front();
q.pop();
sum += node ->val;
if(node ->left != NULL)
q.push(node ->left); if(node ->right != NULL)
q.push(node ->right);
}
res.push_back(sum / len);
}
return res;
}
};

Leetcode637.Average of Levels in Binary Tree二叉树的层平均值的更多相关文章

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

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

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

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

  5. LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)

    637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...

  6. 【Leetcode_easy】637. Average of Levels in Binary Tree

    problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...

  7. 637. Average of Levels in Binary Tree - LeetCode

    Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...

  8. [Swift]LeetCode637. 二叉树的层平均值 | 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 ...

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

随机推荐

  1. JavaScript 数组(Array)方法(二)

    forEach ES5新增的方法,Arr.forEach((value, index,array)=>{}); let arr=['a','b','c']; arr.forEach((val,i ...

  2. Installer - win10安装及卸载SQL Server2008数据库

    一.数据库安装环境 操作系统:win10 SQL server:SQL server 2008 R2 二.全新数据库安装 1.安装扩展文件 双击安装文件,弹出如下窗体:                 ...

  3. Android基础控件RatingBar星级评分条的使用

    1.简介 RatingBar继承ProgressBar,除了ProgressBar的属性外还有特有属性: android:isIndicator:是否用作指示,用户无法更改,默认false andro ...

  4. sed awk 练习

    #定位到某一行 添加内容 lower_case_flag=`cat /etc/my.cnf|grep "^lower_case_table_names"` if [ "X ...

  5. mysql 乐观判断 校验

    说下场景, 用户账户 有 100 元钱,  他执行了两个操作,  A操作发红包发了80块钱, B操作 发红包 发了 70 ,并发, 假如没有 冻结这一说法,  两个操作都是去 查询余额, 还有100 ...

  6. SpringIOC自定义属性编辑器PropertyEditor

    Spring中我们可以使用属性编辑器来将特定的字符串转换为对象 String--转换-->object java.beans.PropertyEditor(JDK中的接口)用于将xml文件中字符 ...

  7. JZOJ5870 【NOIP2018模拟9.15】地图

    题目描述 Description

  8. xshell上传文件到linux

    z,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具. 优点就是不用再开一个sftp工具登录上去上传下载文件. sz:将选定的文件发送(send)到本地机器 rz:运行该 ...

  9. $cordovaNetwork 使用

    1 .安装插件 直接安装: cordova plugin add cordova-plugin-network-information 下载到本地安装: https://github.com/apac ...

  10. Django项目:CRM(客户关系管理系统)--70--60PerfectCRM实现CRM学生上课记录

    #urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...