当前位置:网站首页>【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语言版

边栏推荐
猜你喜欢

Worthington Dissociation Enzymes: Trypsin and Frequently Asked Questions

Vmtouch - under Linux file cache management artifact

9 common mistakes testers fall into

Linux - install MySQL (detailed tutorial)

MATLAB被禁下一个会是LABVIEW吗?国产测试软件ATECLOUD崛起发力

Print linked list from end to beginning

Detailed explanation of nacos cluster configuration

Chinese semantic matching

Worthington酶促细胞收获&细胞粘附和收获

Worthington's tried and tested cell isolation system protocols
随机推荐
Worthington Papain & Chymotrypsin & DNase I
循环神经网络(RNN)
CMake Tutorial Tour(0)_Overview
Recurrent Neural Network (RNN)
How to set up hybrid login in SQL server in AWS
会议OA之待开会议&&所有会议
【经验】经验总结-经验教训
cp强制覆盖与不覆盖拷贝方法
Ubuntu中使用SQLite
Minimum number to rotate array
How Filebeat ensures that the log file is still correctly read when the log file is split (or rolled)
He cell separation technology 丨 basic primary cell separation methods and materials
try_catch捕获异常
遇到bug的解决办法,测试再也不背锅了
npm ERR! code ENOTSUP npm ERR! notsup Unsupported engine for [email protected]: wanted: {“n
每周推荐短视频:研发效能是什么?它可以实现反“内卷”?
Since the media increase play a short video?From the three aspects
Missing X64 mfc140u. DLL file - > application cannot normal boot (0 xc000007b) solution
推荐系统:特征工程、常用特征
CNN的粗浅理解