题目

 1 class Solution {
2 public:
3 vector<double>ans;
4 vector<double> averageOfLevels(TreeNode* root) {
5 if(!root) return ans;
6 queue<TreeNode*>q;
7 q.push(root);
8 while(!q.empty()){
9 int num = q.size();double sum = 0;
10 for(int i = 0;i < num;i++){
11 TreeNode* node = q.front();q.pop();
12 sum += node->val;
13 if(node->left!=NULL) q.push(node->left);
14 if(node->right!=NULL) q.push(node->right);
15 }
16 ans.push_back(sum/num);
17 }
18 return ans;
19 }
20 };

LeetCode637. 二叉树的层平均值的更多相关文章

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

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

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

  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. Java实现 LeetCode 637 二叉树的层平均值(遍历树)

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

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

    Leetcode:637. 二叉树的层平均值 Leetcode:637. 二叉树的层平均值 Talk is cheap . Show me the code . /** * Definition fo ...

  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. Leetcode637.Average of Levels in Binary Tree二叉树的层平均值

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

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

  9. 《程序员代码面试指南》第三章 二叉树问题 二叉树按层打印和ZigZag打印

    题目 二叉树按层打印和ZigZag打印 java代码 package com.lizhouwei.chapter3; import java.util.LinkedList; import java. ...

随机推荐

  1. 一、安装LoadRunner12

    今天接到任务最近要进行性能测试(刚开始搞自动化,有要搞性能测试,领导嫌我不忙吧),之前做接口测试用过Jmeter,也可以使用Jmeter做性能测试,但公司要求用LoadRunner,开始学习性能测试和 ...

  2. CI/CD持续集成方案

    一,CI/CD流程和持续交付简介 CI(Continuous Integration)持续集成  CD(Continuous Deployment)持续部署  CD(Continuous delive ...

  3. elasticsearch-head 配置

    elasticsearch-head install node.js Download node.js: 将下载的包,解压并配置环境变量  vim /etc/profile  ​ #set for n ...

  4. [打基础]luogu2181对角线——计数原理

    啦啦啦我ysw又回来啦!之后大概会准备打acm,暑假尽量复习复习,因为已经快两年没碰oi了,最多也就高三noip前学弟学妹出题讲题,所以从这一篇blog开始大概会有一系列"打基础" ...

  5. Azure Service Bus(二)在NET Core 控制台中如何操作 Service Bus Queue

    一,引言 上一篇讲到关于 Azure ServiceBus 的一些概念,讲到 Azure Service Bus(服务总线),其实也叫 "云消息服务",是微软在Azure 上提供的 ...

  6. Java基础进阶:APi使用,Math,Arrarys,Objects工具类,自动拆装箱,字符串与基本数据类型互转,递归算法源码,冒泡排序源码实现,快排实现源码,附重难点,代码实现源码,课堂笔记,课后扩展及答案

    要点摘要 Math: 类中么有构造方法,内部方法是静态的,可以直接类名.方式调用 常用: Math.abs(int a):返回参数绝对值 Math.ceil(double a):返回大于或等于参数的最 ...

  7. 关于新创公司所需的icp,网文,软著和备案的申请

    刚从一个集团离职来到了创业团队,前期是什么都没有,甚至是公司名字都不知道,哈哈.所以就有了后面的坑踩了一遍又一遍.刚开始是在霍尔果斯注册,结果办icp费了半年的时间,东找西找还没下证.又碰上新疆严查不 ...

  8. PPT技术干货1(上)——设计审美

    序言 PPT直接反映了一个人的能力和态度,PPT能直接反映出老板最看重的4个关键能力: 逻辑思维:全局思考,洞察关键 数据思维:数据分析,指导决策 设计思维:美观大方,彰显专业 工作效率:效率高,出活 ...

  9. 常见数据库的JDBC URL

    转自:http://blog.csdn.net/ring0hx/article/details/6152528 Microsoft SQL Server Microsoft SQL Server JD ...

  10. Oracle RedoLog-基本概念和组成

    Oracle 数据库恢复操作最关键的依据就是 redo log,它记录了对数据库所有的更改操作.在研究如何提取 redolog 中 DML 操作的过程可谓一波三折,因为介绍 redolog 结构细节的 ...