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

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. markdown常用知识点

    为什么要用markdown写开发文档? 1.可以在git上在线预览,docx文档需要下载才能看见: 2. .md文档每次修改之后能被git管理,可追踪修改内容和修改人,但是docx不能追踪修改内容. ...

  2. git 命令行(三)-删除文件

    在Git中,删除也是一个修改操作,我们实战一下,有一个多余的文件:src/common/Util2.js 我们需要删除这个文件, 一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用 rm命令 ...

  3. windows 可执行文件分析

    windows可执行文件是什么? 是具有PE文件格特性的文件,例如:.exe.dll.ocx等文件. 注:(这里只是让大家能明了一些,其实,可执行与否,和后缀没有什么关系,后缀只是windows方便管 ...

  4. dp练习集

    动态规划(DP) // 以下题目来自牛客网 删括号 f[i][j][k] 表示序列s的前i个匹配序列t的前j个,序列s删除部分左括号与右括号数量差为k的情况是否可行 答案为 f[sl][tl][0] ...

  5. Python学习day36-并发编程(2)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  6. neo4j的搭建和实例使用

    一. 简介 neo4j是当今最流行的图数据库,基于 节点+关系 的架构,保存了图形数据的基本元素.同时,数据库也支持通过基础数据元素和独特的CQL查询语法,快速方便的检索.构建复杂的图表关系结果. 二 ...

  7. ElasticSearch入门介绍之会当凌绝顶(一)

    ElasticSearch也是一款非常优秀的开源的全文检索框架,以大名鼎鼎的Apache Lucene为基础,高度封装了更丰富,易用的API,同时与Apache Solr一样,提供了非常强大的分布式集 ...

  8. Tornado Demo1---webspider分析

    Demo源码地址 https://github.com/CHUNL09/tornado/tree/master/demos/webspider 这个Demo的作用是用来获取特定URL的网页中的链接(链 ...

  9. elasticsearch 中文API 记数(八)

    计数API 计数API允许开发者简单的执行一个查询,返回和查询条件相匹配的文档的总数.它可以跨多个索引以及跨多个类型执行. import static org.elasticsearch.index. ...

  10. tDQSS

    tDQSS - DQS latching rising transitions to associated clock edges, as described on Table 41/42 of JE ...