当前位置:网站首页>【Leetcode】637. Layer average of binary tree
【Leetcode】637. Layer average of binary tree
2022-06-12 12:16:00 【Greenary】
Topic link : The layer average of a binary tree
Title Description : Given the root node of a non empty binary tree root , Return the average value of nodes in each layer in the form of array . Differ from the actual answer 10-5 Answers within are acceptable .
analysis : Find the average value of each layer , Summation Right side view of binary tree The idea is similar . Use breadth first search , Sequence traversal , Find the sum of the node values of each layer divided by the number of nodes in that layer , Record the average .
js The code is as follows :
// Use BFS Breadth first search
var averageOfLevels = function(root) {
if (root == null)
return [0];
if(root.left==null && root.right==null)
return [root.val];
let nodeArray = new Array();
nodeArray.push(root);
let valueArray = new Array();
valueArray.push(root.val);
while (nodeArray.length!=0) {
let length=nodeArray.length;
let i=0,sum=0;
while(i < length) {
let left=nodeArray[0].left;
if(left){
nodeArray.push(left);
sum+=left.val;
}
let right=nodeArray[0].right;
if(right){
nodeArray.push(right);
sum+=right.val;
}
i++;
nodeArray.shift();
}
if(nodeArray.length==0)
return valueArray;
valueArray.push(sum/nodeArray.length);
}
};边栏推荐
- Channel Shuffle类
- VGG小卷积代替大卷积 VS 深度可分离卷积
- QML first day
- 作物模型的区域模拟实现过程初探
- Chapter VI data type (V)
- Imx6 uboot add lvds1 display
- The difference between bind, call and apply, and the encapsulation of bind()
- Difference between Definition and Declaration
- Jump instruction of arm instruction set
- promise的理解已经利用promise实现图片的预加载(顺序加载)
猜你喜欢

左右案例+小圆点的轮播图+无时间

LeetCode 1037. Effective boomerang (vector cross product)

Common debugging tools and commands for ROS

Batch load/store instructions of arm instruction set

5G NR協議學習--TS38.211下行通道

LeetCode 1037. 有效的回旋镖(向量叉乘)

ELK搭建指南

Reprint --win10 open the task manager to solve the blue screen problem

QT adds a summary of the problems encountered in the QObject class (you want to use signals and slots) and solves them in person. Error: undefined reference to `vtable for xxxxx (your class name)‘

KDD2022 | 边信息增强图Transformer
随机推荐
Data processing instruction addressing method of arm instruction set
LeetCode_二分搜索_中等_162. 寻找峰值
左右案例+小圆点的轮播图+无时间
导航中,添加边框影响布局的解决方法
Longest string without duplicate characters (leetcode 3)
爱可可AI前沿推介(6.12)
AGCO AI frontier promotion (6.12)
拿来就能用的网页动画特效,不来看看?
A short guide to SSH port forwarding
VGG小卷积代替大卷积 VS 深度可分离卷积
Open source project - (erp+ Hotel + e-commerce) background management system
Advantages and disadvantages of single page development and multi page development
宏编译 预处理头 WIN32_LEAN_AND_MEAN
用cloneNode 克隆,解决id问题/方法 深复制和浅复制修改id的方法
创建Servlet项目
LeetCode_ String_ Simple_ 344. reverse string
promise的理解已经利用promise实现图片的预加载(顺序加载)
AND THE BIT GOES DOWN: REVISITING THE QUANTIZATION OF NEURAL NETWORKS
IP address management
ARP protocol data processing process of neighbor subsystem