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. ...
随机推荐
- centos 7 配置 mysql 5.7 主从复制
centos 7 配置 mysql 5.7 主从复制 主库:192.168.12.3 从库:192.168.12.2 1. 主库从库所在服务器关闭防火墙Systemctl stop firewalld ...
- Linux shell 获取目录下时间最新的文件的文件名
ls -lt /dirname/ | grep filename | head -n 1 |awk '{print $9}' 逐条解释: ls -lt /dirname/ 列出此目录下的所有文件并按照 ...
- PDCA
Plan(规划) Do(执行) Check(验证) Adjust(调整)
- 安卓 Android Studio 下载
http://www.android-studio.org/ 下载地址 https://blog.csdn.net/qq_41976613/article/details/91432304 ...
- 01-flask-helloWorld
代码 from flask import Flask # 创建Flask对象 app = Flask(__name__) # 定义路由 @app.route('/') def index(): # 函 ...
- ActiveMq反序列化漏洞(CVE-2015-5254)漏洞复现
漏洞原理 Apache ActiveMQ 5.13.0之前5.x版本中存在安全漏洞,该漏洞源于程序没有限制可在代理中序列化的类.远程攻击者可借助特制的序列化的Java Message Service( ...
- Spring Cloud Alibaba基础教程-Nacos(一)
2019快结束,也有很久没写博客了,今天我们来谈谈Nacos,如果对您有帮助,麻烦左上角点个关注 ,谢谢 ! 嘻嘻 今天先写第一篇 文章目录 为什么要使用Nacos Eureka 闭源 Nacos的优 ...
- Python代码实现猜数字游戏
1 # -*- coding:utf-8 -*- 2 # 作者:IT小学生蔡坨坨 3 # 时间:2020/12/9 23:02 4 5 # 猜数字游戏: 6 # 代码中生成一个随机整数. 7 # 然后 ...
- H3C S5120V2-SI 交换机配置
连接终端线 可以看到开机信息 ......................................................................Done. System is ...
- SQL语句中case,when,then的用法
用法如下bai: 复制代码 SELECT s.s_id, s.s_name, s.s_sex, CASE WHENs.s_sex='1'THEN'男' WHENs.s_sex='2'THEN'女' E ...