Leetcode:637. 二叉树的层平均值
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. 二叉树的层平均值的更多相关文章
- LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)
637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...
- Java实现 LeetCode 637 二叉树的层平均值(遍历树)
637. 二叉树的层平均值 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7 输出: [3, 14.5, 11] 解释: 第0层的 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- Leetcode637.Average of Levels in Binary Tree二叉树的层平均值
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. class Solution { public: vector<double> averageOfLevels(TreeNode ...
- LeetCode637. 二叉树的层平均值
题目 1 class Solution { 2 public: 3 vector<double>ans; 4 vector<double> averageOfLevels(Tr ...
- LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
随机推荐
- UiPath 中 List 集合的实例化与使用
>>>跳过BB,空降正文<<< 目录 前言 正文 1. 创建 List 变量 2. 实例化 List 变量 3. 集合的使用 后记 前言 大家好呀,我是 白墨,一个 ...
- MySQL笔记02(黑马)
DDL操作数据库.表 操作数据库:CRUD C(Create):创建 创建数据库: create database 数据库名称; 创建数据库,判断不存在,再创建: create database if ...
- 【C++】解决c++中cout输出中文乱码问题
问题:cout输出中文乱码.例如下面的代码输出会乱码. cout << "成功!" << endl; 输出结果: 解决方案: 控制台还原旧版即可,打开程序- ...
- 【SQLite】教程07-C/C++上使用SQLite3
1.配置好C/C++项目环境 2.源码 1 #include <iostream> 2 #include <vector> 3 #include <string> ...
- 「JVM」知识点详解一:JVM运行原理详解
前言 JVM 一直都是面试的必考点,大家都知道,但是要把它搞清楚又好像不是特别容易.JVM 的知识点太散,不系统,今天带大家详细的了解一下jvm的运行原理. 正文 1 什么是JVM? JVM是Java ...
- bzoj2427 软件安装! 树dp
软件安装 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一 些软 ...
- 精通LED驱动芯片HT1632C指令与编程应用
HT1632C是一款很常用的LED(数码管或点阵)驱动芯片,虽然官方已经宣布该芯片明年(2021年)即将寿终正寝(停产),但是相同厂家生产的同系列芯片的控制方式通常是相同的(事实上,大多数LED驱动芯 ...
- div和img垂直居中的方法
div垂直居中可以使用height和line-height,多个div的话就不适用了. 可以使用下面的方式垂直居中 <div class="parent"> <d ...
- ClickHouse学习系列之六【访问权限和账户管理】
背景 在之前写的文章[用户权限管理]里已经介绍了应该如何设置用户密码以及权限控制.但是只是针对修改配置文件的方式来进行用户权限管理,其实ClickHouse也支持基于RBAC(Role-Based A ...
- 36、网卡绑定bond
注意:虚拟机需要网卡模式为同一模式,否则无法进行通信: 36.1.mode0(平衡负载模式): 平时两块网卡均工作,且自动备援,但需要在与服务器本地网卡相连的交换机设备上进行端口聚合来支持绑定技术. ...