[Leetcode] 637. Average of Levels in Binary Tree

Microapp 接入vite子应用 MicroApp接入vite版本,干货满满,手把手步骤说明,加上报错说明,赶紧上车! 阅读详情

Description:

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

Example 1:

Input:
    3
   / \
  9  20
    /  \
   15   7
Output: [3, 14.5, 11]
Explanation:
The average value of nodes on level 0 is 3,  on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].

 

Note:

  1. The range of node's value is in the range of 32-bit signed integer.

 

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List <Double> result = new ArrayList<>();
        Queue<TreeNode> test = new LinkedList<>();
        test.add(root);
        while (!test.isEmpty()){
            int num = test.size();
            double sum = 0.0;
            for (int i = 0; i < num; i++) {
                TreeNode x = test.poll();
                sum = sum + x.val;
                if (x.left != null)
                    test.add(x.left);
                if (x.right != null)
                    test.add(x.right);                               
            }
            result.add(sum/num);
        }
        return result;
    }
}

 

p.s. 这里使用了queue来统计每一层element的个数!

 

ArcGIS属性表汉字转拼音 接着我安装了一个编辑器“PyCharm Community Edition 2019.3.3\bin\pycharm64.exe”,在里面添加“pypinyin”包,然后将其设置为arcgis的编辑器和调试程序,还是失败;我在10.6的机器上折腾了上面几步,直到两周后脚本突然运行成功,我也不确定是哪一步导致pypinyin包配置成功,因此将心得体会写出来,如果过一段时间,打开10.2的机器运行脚本成功了,再写后续.......,按照“配置方法1:”进行尝试,显示配置失败。 阅读详情

相关推荐

轨迹规划——带抛物线过渡的直线插补

带抛物线过渡的直线插补

doudou2weiwei的博客 1986

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 array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explan

淡然坊 2296

Cadence 17.2配置CIS数据库报错?手把手解决ODBC驱动位数不匹配问题

本文详细解析了Cadence 17.2配置CIS数据库时常见的ODBC驱动位数不匹配问题,提供了从系统环境检查到分步解决方案的完整指南。重点解决32位应用程序与64位ODBC驱动之间的架构冲突,包含驱动安装、DSN配置及高级排查技巧,帮助电子工程师快速恢复设计环境。

play7的博客 972

【Leetcode】637. Average of Levels in Binary Tree

求二叉树每一层的平局值 思路 其实就是层次遍历的方法,再除以每层的节点数就行了,层次遍历的各种写法都可以用 方法1 深搜非递归 class Solution1(object): def averageOfLevels(self, root): &quot;&quot;&quot; :type root: TreeNode :rtype: List[float] ...

很吵请安静 451

流媒体学习之路(mediasoup)——simulcast 与 svc 简介(7)

流媒体学习之路(mediasoup)——simulcast 与 svc(7) 文章目录流媒体学习之路(mediasoup)——simulcast 与 svc(7)

qw225967的博客 7342

用PyTorch手把手搭建液态神经网络(LNN):从微分方程到无人机避障实战

本文详细介绍了如何使用PyTorch搭建液态神经网络(LNN),从微分方程基础到无人机避障实战应用。LNN通过动态时序建模和微分方程实现高效计算,特别适合多传感器数据融合场景。文章包含完整的代码实现、训练技巧及性能优化方案,帮助开发者快速掌握这一前沿技术。

uber9的博客 441

Leetcode 637 Average of Levels in Binary Tree

题目链接: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 array. Example 1: Input: 3...

Mereder的博客 181

机器视觉工程师如何进行高速运动物体捕获

硬件方面使用高帧率相机(如工业相机、高速摄像机)确保充足的光照条件使用高性能计算平台减少处理延迟软件方面背景减除技术快速分离运动物体卡尔曼滤波预测物体轨迹ROI技术减少计算量多线程处理提高实时性实际应用中的关键考量针对特定场景调整参数(如阈值、滤波器参数等)平衡计算资源与实时性要求结合特定领域知识优化算法(如物体运动特性)上述示例代码实现了一个基础的高速物体跟踪系统,通过背景减除和卡尔曼滤波实现对高速物体的检测和跟踪。

zhangzhechun_02的博客 626

LeetCode 637. Average of Levels in Binary Tree

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 array. Example 1: Input: 3 / \ 9 20 ...

weixin_30648963的博客 272

YOLOv5源码逐行超详细注释与解读(2)——推理部分detect.py

全网最详细的YOLOv5之推理部分detect.py源码逐行注释,近四万字的超详细讲解!小白入门必看!

路人贾的博客 5万+

抖音无水印视频爬虫实战:从抓包分析到批量下载指定用户作品

抖音作为当前最受欢迎的短视频平台,拥有海量的优质内容。很多用户希望备份自己或他人的作品,但抖音官方并未提供批量下载功能,且直接下载的视频带有平台水印。本文将带领大家从零开始,通过抓包分析、设备模拟、请求签名等核心技术,实现一个完整的抖音无水印视频爬虫系统。本文仅供技术学习和个人备份使用,请勿将爬取的内容用于商业用途或侵犯他人版权,爬取频率请控制合理范围内。目录一、前言二、技术栈概览三、准备工作3.1 环境搭建3.2 抓包分析步骤3.3 关键API发现四、核心技术解密4.1 X-Gorgon签名算法。

2201_76125261的博客 847

[LeetCode][637]Average of Levels in Binary Tree题解

[LeetCode][637]Average of Levels in Binary Tree题解 标签:BFS queue 题意:求树的每层的平均数 使用bfs即可 code: /* * [637] Average of Levels in Binary Tree * * https://leetcode.com/problems/average-of-levels-in-binary-...

OCEANtroye的博客 299

手把手教你学Simulink——基于经典PID控制的电机系统场景实例:永磁同步电机(PMSM) PID转速控制仿真

*永磁同步电机(PMSM)因其高效、高功率密度和良好的动态响应特性,在现代工业自动化、电动汽车驱动系统等领域得到了广泛应用。为了实现对PMSM的速度精确控制,通常采用矢量控制(Field Oriented Control, FOC)**策略,其中PID控制器是实现速度环和电流环调节的关键组件。本教程将带你使用构建一个基于PID控制的永磁同步电机转速控制系统仿真模型PMSM电机建模矢量控制(FOC)策略实现PID控制器设计系统仿真与调试控制效果分析本文介绍了如何使用构建一个。

MHD0815的博客 6953

Leetcode——637. Average of Levels in Binary Tree

题目原址 https://leetcode.com/problems/average-of-levels-in-binary-tree/description/ 题目描述 Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. E...

想当厨子的程序媛 289

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

[LeetCode] 637. Average of Levels in Binary Tree

原题链接: https://leetcode.com/problems/average-of-levels-in-binary-tree/ 1. 题目介绍 Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. 给出一个非空的二叉树,返回每...

Ber03的博客 267

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode 637 average of levels in binary tree

just use level traverse and calculate the average of each level. class Solution { public List<Double> averageOfLevels(TreeNode root) { List<Double> final_res = new ArrayList<>(); //List<List<Integer>> res =

Tech in Pieces 319

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

[leetcode]637. Average of Levels in Binary Tree(Python)

[leetcode]637. Average of Levels in Binary Tree题目描述题目理解解题思路DFSBFSMyMethodTime 题目描述 Category:Easy Tree Given a non-empty binary tree, return the average value of the nodes on each level in the form of...

La_Ragazza的博客 484

[LeetCode]637. Average of Levels in Binary Tree

[LeetCode]637. Average of Levels in Binary Tree题目描述思路实际上是树的层次遍历 递归写法参数中加入层数即可代码#include <iostream> #include <vector>using namespace std;struct TreeNode { int val; TreeNode* left; TreeNode*

Lcharon的博客 834

【Leetcode】637. Average of Levels in Binary Tree(Easy)

1.题目Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Example 1:Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanatio...

不想做菜鸟的小螃蟹 333

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 array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 1...

lxin_liu的博客 142

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 array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11]...

ahdctjv534913的博客 97

【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 array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanati...

ghscarecrow的博客 174

27.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 array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11]

wjx_sky的博客 279

Leetcode 637. Average of Levels in Binary Tree (python+cpp)

Leetcode 637. Average of Levels in Binary Tree题目解法: 题目 解法: 这其实就是层级遍历,一般用bfs实现,两个循环嵌套,内循环负责遍历一层上所有节点 python代码如下: class Solution: def averageOfLevels(self, root: TreeNode) -> List[float]: q = collections.deque(); q.append(root)

qq_37821701的博客 286
上一篇: [Leetcode] 136. Single Number
下一篇: [Leetcode] 693. Binary Number with Alternating Bits
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值