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. ...
随机推荐
- 【ubuntu-18.04】ubuntu18.04进行Nvidia显卡配置
转自https://blog.csdn.net/qq_37935670/article/details/80377196 2.显卡驱动配置 网上有些攻略非常非常复杂,又要禁用nouveau驱动,又要进 ...
- 精尽Spring MVC源码分析 - MultipartResolver 组件
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- 遍历出字母A-Z(a-z)的四种方式
# 四种方式打印出A-Z(a-z) import string l1 = [chr(i) for i in range(ord("A"), ord("Z") + ...
- Python SQLALchemy框架
SQLALchemy SQLALchemy是Python中的一款优秀的ORM框架,它可以作用于任何第三方Web框架,如flask,tornado等框架. SQLALchemy相较于DjangoORM来 ...
- Shell-sed之替换字符
将A_B_C_D_TXT 修改为 A.B_C_D.TXT echo "A_B_C_D_TXT"|sed 's/_/./1;s/_/./3' 注:后面的数字代表匹配的次数, 1表示第 ...
- python去除文件中重复的行
去除文件中重复的行 import os with open('db.txt','r',encoding='utf-8') as read_f,\ open('.db.txt.swap','w',enc ...
- Web服务器-服务器开发-返回浏览器需要的页面 (3.3.2)
@ 目录 1.说明 2.代码 关于作者 1.说明 使用正则表达式,匹配客户端的请求头 获取到请求的路径 返回对应请求路径的文字 可以使用打开对应文件的方式去返回对应的文件 2.代码 from sock ...
- python简单的函数应用
一个简单的函数应用,包括自定义函数,lambda函数,列表解析. 1 #!usr/bin/env python3 2 # -*- coding:utf-8 -*- 3 4 #开始定义函数 5 def ...
- Autofac的基本使用---目录
目录 Autofac的基本使用---1.前言 Autofac的基本使用---2.普通类型 Autofac的基本使用---3.泛型类型 Autofac的基本使用---4.使用Config配置 Autof ...
- SpringBoot整合任务调度框架Quartz及持久化配置
目录 本篇要点 SpringBoot与Quartz单机版快速整合 引入依赖 创建Job 调度器Scheduler绑定 自动配置,这里演示SimpleScheduleBuilder 手动配置,这里演示C ...