LeetCode637. 二叉树的层平均值
题目
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. 二叉树的层平均值的更多相关文章
- [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 ...
- LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)
637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...
- 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 ...
- Java实现 LeetCode 637 二叉树的层平均值(遍历树)
637. 二叉树的层平均值 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7 输出: [3, 14.5, 11] 解释: 第0层的 ...
- Leetcode:637. 二叉树的层平均值
Leetcode:637. 二叉树的层平均值 Leetcode:637. 二叉树的层平均值 Talk is cheap . Show me the code . /** * Definition fo ...
- [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 ...
- Leetcode637.Average of Levels in Binary Tree二叉树的层平均值
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. class Solution { public: vector<double> averageOfLevels(TreeNode ...
- [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 ...
- 《程序员代码面试指南》第三章 二叉树问题 二叉树按层打印和ZigZag打印
题目 二叉树按层打印和ZigZag打印 java代码 package com.lizhouwei.chapter3; import java.util.LinkedList; import java. ...
随机推荐
- [日常摸鱼]bzoj1470[noi2002]Savage
晚上做到的一个扩欧的水题(?) wa了好几发感觉自己药丸-重新推了一遍公式才发现自己打错了orz 借此复习一下扩欧吧-orz 题目:http://www.lydsy.com/JudgeOnline/p ...
- CSRF学习
前提环境:网站存在CSRF漏洞(也就是过于相信访问请求,只判断了用户是否存在cookie,并未判断请求的发起者) CSRF攻击原理,用户A需要转账,用户A正常向银行网站发送请求登录,登录成功后银行网站 ...
- 关于python面试中的设计模式,搞懂这些就足够了
1.什么是设计模式? 设计模式是经过总结.优化,对我们经常遇到的一些编程问题的可重用的解决方案.设计模式不同于类或库可直接作用于代码.相反,它更为的高级,是一种必须在特定的情形下实现的方法模版. 2. ...
- python pip命令的安装与实验安装scrapy
大家在使用python时候,很多时候导入模块都会发现该模块不存在,那么我们就需要下载安装,可是有时候安装会出现各种问题,大家回去请教别人,大部分程序员会回答你:pip install 什么等,可是你p ...
- Flutter ListTile - Flutter每周一组件
该文章属于[Flutter每周一组件]系列,其它组件可以查看该系列下的文章,该系列会不间断更新:所有组件的demo已经上传值Github: https://github.com/xj124456/fl ...
- 安装篇四:安装NGINX(1.4.0版本)
#1.NGINX安装 1.安装文件上传软件 [root@TEST ~]# yum install lrzsz –y <---拖拽文件 2.检查软件安装的系统环境 [root@TEST ~]# c ...
- 简单谈谈contextlib的使用
简单谈谈contextlib的使用 写在前面 做这件事的原因: 在看书的时候,我发现了有大佬们用contextlib管理上下文,真的很牛皮,但是百度了以下,每个大佬都写了很多很全很深刻,讲道理五花八门 ...
- Java 8 新特性 - Lambda表达式
Lambda表达式 vs 匿名类既然lambda表达式即将正式取代Java代码中的匿名内部类,那么有必要对二者做一个比较分析.一个关键的不同点就是关键字 this.匿名类的 this 关键字指向匿名类 ...
- 关于eclipse反编译插件不起作用问题的解决
1.首先我的eclipse版本是 Version: Photon Release (4.8.0),小伙伴们可以通过 help>>About eclipse IDE 来查看自己的eclips ...
- Android驱动学习-内部机制_回顾binder框架关键点
内部机制_回顾binder框架关键点server注册服务时, 对每个服务都提供不同的ptr/cookie,在驱动程序里对每个服务都构造一个binder_node, 它也含有ptr/cookie cli ...