当前位置:网站首页>【LeetCode每日一题】——637.二叉树的层平均值
【LeetCode每日一题】——637.二叉树的层平均值
2022-07-30 00:52:00 【IronmanJay】
一【题目类别】
- 二叉树
二【题目难度】
- 简单
三【题目编号】
- 637.二叉树的层平均值
四【题目描述】
- 给定一个非空二叉树的根节点 root , 以数组的形式返回每一层节点的平均值。与实际答案相差 1 0 − 5 10^{-5} 10−5 以内的答案可以被接受。
五【题目示例】
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[3.00000,14.50000,11.00000]
解释:第 0 层的平均值为 3,第 1 层的平均值为 14.5,第 2 层的平均值为 11 。
因此返回 [3, 14.5, 11] 。示例 2:
输入:root = [3,9,20,15,7]
输出:[3.00000,14.50000,11.00000]
六【题目提示】
- 树中节点数量在 [ 1 , 1 0 4 ] 范围内 树中节点数量在 [1, 10^4] 范围内 树中节点数量在[1,104]范围内
- − 2 31 < = N o d e . v a l < = 2 31 − 1 -2^{31} <= Node.val <= 2^{31} - 1 −231<=Node.val<=231−1
七【解题思路】
- 本题思路很简单,看到层相关的二叉树的题目,就应该立马想到BFS(广度优先遍历)
- 所以手写一个队列,将每层的值依次求平均值放到结果数组中即可,难度不大
- 要注意类型转换,求平均值的时候要使用double类型,否则会损失精度
八【时间频度】
- 时间复杂度: O ( n ) O(n) O(n),其中 n n n为树的结点个数
- 空间复杂度: O ( n ) O(n) O(n),其中 n n n为树的结点个数
九【代码实现】
- Java语言版
package Tree;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class p637_LevelAverageOfBinaryTrees {
int val;
p637_LevelAverageOfBinaryTrees left;
p637_LevelAverageOfBinaryTrees right;
public p637_LevelAverageOfBinaryTrees() {
}
public p637_LevelAverageOfBinaryTrees(int val) {
this.val = val;
}
public p637_LevelAverageOfBinaryTrees(int val, p637_LevelAverageOfBinaryTrees left, p637_LevelAverageOfBinaryTrees right) {
this.val = val;
this.left = left;
this.right = right;
}
public static void main(String[] args) {
p637_LevelAverageOfBinaryTrees root = new p637_LevelAverageOfBinaryTrees(3);
p637_LevelAverageOfBinaryTrees left = new p637_LevelAverageOfBinaryTrees(9);
p637_LevelAverageOfBinaryTrees right = new p637_LevelAverageOfBinaryTrees(20);
p637_LevelAverageOfBinaryTrees right1 = new p637_LevelAverageOfBinaryTrees(15);
p637_LevelAverageOfBinaryTrees right2 = new p637_LevelAverageOfBinaryTrees(7);
root.left = left;
root.right = right;
right.left = right1;
right.right = right2;
List<Double> res = averageOfLevels(root);
System.out.println("res = " + res);
}
public static List<Double> averageOfLevels(p637_LevelAverageOfBinaryTrees root) {
List<Double> res = new ArrayList<>();
Queue<p637_LevelAverageOfBinaryTrees> queue = new LinkedList<>();
queue.add(root);
while (queue.size() != 0) {
double sum = 0;
int len = queue.size();
for (int i = 0; i < len; i++) {
p637_LevelAverageOfBinaryTrees temp = queue.poll();
sum += temp.val;
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
}
res.add(sum / len);
}
return res;
}
}
- C语言版
#include<stdio.h>
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
double* averageOfLevels(struct TreeNode* root, int* returnSize)
{
double* res = (double*)malloc(sizeof(double) * 10001);
int index = 0;
struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 10001);
int front = 0;
int rear = 0;
queue[rear++] = root;
while (front != rear)
{
double sum = 0;
int size = (rear - front + 10001) % 10001;
for (int i = 0; i < size; i++)
{
struct TreeNode* temp = queue[front++];
sum += temp->val;
if (temp->left != NULL)
{
queue[rear++] = temp->left;
}
if (temp->right != NULL)
{
queue[rear++] = temp->right;
}
}
res[index++] = sum / size;
}
*returnSize = index;
return res;
}
/*主函数省略*/
十【提交结果】
Java语言版
C语言版
边栏推荐
猜你喜欢
[Flutter] Detailed explanation of the use of the Flutter inspector tool, viewing the Flutter layout, widget tree, debugging interface, etc.
Detailed explanation of nacos cluster configuration
记笔记!电源自动测试系统测试电源纹波详细方法
基于SSM开发实现校园疫情防控管理系统
MATLAB被禁下一个会是LABVIEW吗?国产测试软件ATECLOUD崛起发力
这是一道非常有争议的题,我的分析如下: TCP/IP在多个层引入了安全机制,其中TLS协议位于______。 A.数据链路层 B.网络层 C.传输层 D.应用层
Print linked list from end to beginning
泰克Tektronix示波器软件TDS1012|TDS2002|TDS2004上位机软件NS-Scope
Worthington Optimized Technology: Cell Quantification
Since the media how to write a short video title?Three hot style title, let your video gain more traffic
随机推荐
Detailed introduction of @RequestParam annotation
【mysql】Mysql公用表表达式with as
Worthington Enzymatic Cell Harvest & Cell Adhesion and Harvest
Worthington's tried and tested cell isolation system protocols
How to increase account weight?3 ways to operate your own media to help you get more revenue
抖音短视频流量获取攻略,掌握好这些一定可以出爆款
自学HarmonyOS应用开发(56)- 用Service保证应用在后台持续运行
FlutterBoost 3.0出现 Activity无法转换为ExclusiveAppComponent<Activity>的解决办法
canvas 中如何实现物体的框选(六)
重建二叉树
自学HarmonyOS应用开发(53)- 获取当前位置
string replace spaces
Low dropout linear regulator MPQ2013A-AEC1 brand MPS domestic replacement
自学HarmonyOS应用开发(47)- 自定义switch组件
新媒体运营必备的4个热点查询网
Since the media how to write a short video title?Three hot style title, let your video gain more traffic
[Flutter] Detailed explanation of the use of the Flutter inspector tool, viewing the Flutter layout, widget tree, debugging interface, etc.
Running a Fabric Application
3 tips for using hot events to create press releases?A must-see for self-media people
软考 --- 数据库(5)数据库控制