目录
1,题目
给定一个非空二叉树的根节点 root
, 以数组的形式返回每一层节点的平均值。
2,代码
2.1广度优先遍历
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var averageOfLevels = function(root) {
// 广度优先遍历
// 模拟队列
let ans = [];
let queues = [];
queues.push(root);
while(queues.length > 0){
let size = queues.length;
let numSize = size;
let sum = 0;
while(size > 0){
let node = queues.shift();
size--;
sum += node.val;
if(node.left != null){
queues.push(node.left);
}
if(node.right != null){
queues.push(node.right);
}
}
ans.push(sum / numSize);
}
return ans;
};
自己完成的
一时忽略了size的减1 变化 导致代码出错
略做优化 ,size变量的使用
var averageOfLevels = function(root) {
// 广度优先遍历 (Breadth-first Search)
// 模拟队列 (Simulate queue)
let ans = [];
let queues = [];
queues.push(root);
while (queues.length > 0) {
let size = queues.length; // Nodes count at the current level
let sum = 0; // Sum of node values at the current level
for (let i = 0; i < size; i++) { // Ensure we only process the current level
let node = queues.shift(); // Remove and process the front node in the queue
sum += node.val; // Add the node's value to the sum
// Add child nodes to the queue for processing in the next level
if (node.left != null) {
queues.push(node.left);
}
if (node.right != null) {
queues.push(node.right);
}
}
ans.push(sum / size); // Calculate and store the average value for this level
}
return ans; // Return the list of averages
};
2.2 深度优先遍历
/**
* @param {TreeNode} root
* @return {number[]}
*/
var averageOfLevels = function(root) {
dfs =(root ,level ,sums,counts)=>{
if(root === null){
return;
}
if(level<sums.length){
sums[level] += root.val;
counts[level] += 1;
}else{
sums.push(root.val);
counts.push(1);
}
dfs(root.left,level+1,sums,counts);
dfs(root.right,level+1,sums,counts);
}
// 深度优先遍历
let sums = [];
let counts = [];
dfs(root, 0 , sums, counts);
let averages = [];
for(let i=0; i<sums.length; i++){
averages.push(sums[i] / counts[i]);
}
return averages;
};
带注释版
class Solution {
averageOfLevels(root) {
let counts = []; // 用来追踪每一层的节点数
let sums = []; // 用来追踪每一层的节点值总和
this.dfs(root, 0, counts, sums);
let averages = []; // 用来存储每一层节点的平均值
for (let i = 0; i < sums.length; i++) {
averages.push(sums[i] / counts[i]); // 计算平均值并添加到数组
}
return averages;
}
dfs(node, level, counts, sums) {
if (node === null) {
return;
}
if (level < sums.length) {
sums[level] += node.val; // 更新总和
counts[level] += 1; // 更新节点数
} else {
sums.push(node.val); // 添加新的层级总和
counts.push(1); // 添加新的层级节点数
}
this.dfs(node.left, level + 1, counts, sums); // 递归遍历左子树
this.dfs(node.right, level + 1, counts, sums); // 递归遍历右子树
}
}
3,学习与总结
深度优先遍历逻辑回顾
- 搜索过程中需要记录当前节点所在层: 初始从0层开始递归遍历,没深入一次,层数level+1;
- 访问到第level层,判断是否有该层的信息:level < sums.length
- 如果已有记录,则进行累加;
- 如果第一次遍历该层,就为这个层级添加一个新的记录;
- 内部要继续调用dfs函数,实现对整棵树的 遍历;
- if( root === null )在递归中 充当 结束的条件,和判断 初始输入的数是否为空 不一样;
勉励自己:贵在坚持!
标签:node,Days19,level,queues,637,sums,let,二叉树,root From: https://blog.csdn.net/m0_51666362/article/details/136732958