Leetcode:637. 二叉树的层平均值

Leetcode:637. 二叉树的层平均值

Talk is cheap . Show me the code .

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> ans;
queue<TreeNode*> que;
TreeNode* node;
int size=0;
double temp=0;
if(root!=NULL) que.push(root);
while(!que.empty()){
size=que.size();
for(int i=0;i<size;i++){
node=que.front();
que.pop();
temp+=node->val;
if(node->left!=NULL) que.push(node->left);
if(node->right!=NULL) que.push(node->right);
}
ans.push_back(temp/size);
temp=0;
}
return ans;
}
};

Leetcode:637. 二叉树的层平均值的更多相关文章

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

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

  2. Java实现 LeetCode 637 二叉树的层平均值(遍历树)

    637. 二叉树的层平均值 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7 输出: [3, 14.5, 11] 解释: 第0层的 ...

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

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

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

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

  7. Leetcode637.Average of Levels in Binary Tree二叉树的层平均值

    给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. class Solution { public: vector<double> averageOfLevels(TreeNode ...

  8. LeetCode637. 二叉树的层平均值

    题目 1 class Solution { 2 public: 3 vector<double>ans; 4 vector<double> averageOfLevels(Tr ...

  9. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

随机推荐

  1. Win10 下python_appium的Android手机自动化环境搭建

    前提: 已经安装好了Java环境,且配置了环境变量 已经安装python3.8.2,已经安装pycham. 一.安装appium_client ,pycham中也需要安装 二.安装node.js(需要 ...

  2. Java设计模式:23种设计模式全面解析(超级详细)以及在源码中的应用

    从网络上找的设计模式, 很全面,只要把UML类图看懂了, 照着类图将代码实现是很容易的事情. 步骤: 先看懂类图, 然后将代码实现, 之后再看文字 http://c.biancheng.net/des ...

  3. Transformers for Graph Representation

    Do Transformers Really Perform Badfor Graph Representation? microsoft/Graphormer: This is the offici ...

  4. docker0-常用命令-持续更新

    问君哪得清如许,为有源头活水来 1,帮助命令 docker version docker info docker 命令 --help 2,仓库\镜像 docker images 查看所有本地镜像 do ...

  5. Java synchronized对象级别与类级别的同步锁

    Java synchronized 关键字 可以将一个代码块或一个方法标记为同步代码块.同步代码块是指同一时间只能有一个线程执行的代码,并且执行该代码的线程持有同步锁.synchronized关键字可 ...

  6. 解Bug之路-ZooKeeper集群拒绝服务

    解Bug之路-ZooKeeper集群拒绝服务 前言 ZooKeeper作为dubbo的注册中心,可谓是重中之重,线上ZK的任何风吹草动都会牵动心弦.最近笔者就碰到线上ZK Leader宕机后,选主无法 ...

  7. NOIP模拟测试14「旋转子段·走格子·柱状图」

    旋转子段 连60分都没想,考试一直肝t3,t2,没想到t1最简单 我一直以为t1很难,看了题解发现也就那样 题解 性质1 一个包含a[i]旋转区间值域范围最多为min(a[i],i)----max(a ...

  8. Golang控制子gorutine退出,并阻塞等待所有子gorutine全部退出

    Golang控制子gorutine退出,并阻塞等待所有子gorutine全部退出 需求 程序有时需要自动重启或者重新初始化一些功能,就需要退出之前的所有子gorutine,并且要等待所有子goruti ...

  9. excel VBA数组运用

    Sub a()Dim i人数 As Integer'定义变量Dim i考试成绩() As Integer'定义数组Dim i As Integer'定义变量i人数 = InputBox("输 ...

  10. AvtiveMQ与SpringBoot结合

    首先来了解下ActivieMQ的应用场景,消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题.实现高性能,高可用,可伸缩和最终一致性架构是大型分布式系统不可缺少的中间件 ...