首页 > 编程语言 >LeetCode-Java-637. Average of Levels in Binary Tree

LeetCode-Java-637. Average of Levels in Binary Tree

时间:2022-12-14 15:06:44浏览次数:54  
标签:node Binary right TreeNode 637 int sum tree Java


题目

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:
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) {
ArrayList<Double> list = new ArrayList<Double>();
LinkedList<TreeNode> tree = new LinkedList<TreeNode>();
tree.add(root);
double sum = 0;
while(!tree.isEmpty()){
int n = tree.size();
sum = 0;
for(int i=0;i<n;i++){
TreeNode node = tree.removeFirst();
sum+=node.val;
if(node.left!=null){
tree.addLast(node.left);
}
if(node.right!=null){
tree.addLast(node.right);
}
}
list.add(sum/n);
}
return list;
}
}

/**
* 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) {
ArrayList<Double> list = new ArrayList<Double>();
LinkedList<TreeNode> tree = new LinkedList<TreeNode>();
tree.add(root);
double sum = 0;
while(!tree.isEmpty()){
int n = tree.size();
sum = 0;
for(int i=0;i<n;i++){
TreeNode node = tree.removeFirst();
sum+=node.val;
if(node.left!=null){
tree.addLast(node.left);
}
if(node.right!=null){
tree.addLast(node.right);
}
}
list.add(sum/n);
}
return list;
}
}


标签:node,Binary,right,TreeNode,637,int,sum,tree,Java
From: https://blog.51cto.com/u_12938555/5936978

相关文章

  • Java泛型链表实现
    ​​链表简介​​代码实现packagecom.chenxixuexi;/***泛型链表*单链表逆置数据--节点*求单链表倒数第K节点*求两个单链表是否相交相交交点*判断单链表是否有......
  • 15-咸鱼学Java-内部类
    实例内部类实例内部类简而言之就是类里面嵌入着类,就像环套环一样Demo:classOuterClass2//外部类{privateinta=1000;publicOuterClass2(){Sy......
  • LeetCode-Java-559. Maximum Depth of N-ary Tree
    题目Givenan-arytree,finditsmaximumdepth.Themaximumdepthisthenumberofnodesalongthelongestpathfromtherootnodedowntothefarthestleafnode......
  • 14-咸鱼学Java-面向对象基础:类
    类类就相当于自定义类型,有自己的数据域,有自己的方法。属于一种用户自定义类型。类的目的就是模拟现实中存在的物体,如一个Person类,一个人他有自己的名字,年龄,性别等等,他有自己......
  • 17-咸鱼学Java-内部类实际应用-Java链表
    ​​内部类​​,​​未使用内部类的链表​​可以点击名字查看。此篇文章主要介绍运用内部类的Java链表的写法。链表正如其名,就像一个一个珠子被串起来,只有前一个珠子和......
  • LeetCode-Java-876. Middle of the Linked List
    题目Givenanon-empty,singlylinkedlistwithheadnodehead,returnamiddlenodeoflinkedlist.Iftherearetwomiddlenodes,returnthesecondmiddlenode.......
  • 咸鱼学Java-多线程基础
    实现多线程的方法1.继承Thread,并且重写run方法(线程体)2.实现Runnable接口,实现run方法(线程体)必须重写run1.继承ThreadclassMyThreadextendsThread{publicMyThr......
  • 剑指Offer-Java-用两个栈实现队列
    题目用两个栈来实现一个队列,完成队列的Push和Pop操作。队列中的元素为int类型。代码只需要来回倒就可以实现了。importjava.util.Stack;publicclassSolution{Stack......
  • 23-咸鱼学Java-Java中的栈
    栈的图示代码/***栈*@author焦焱**/publicclassStack{/***栈顶标记*/inttop;/***栈内元素*/int[]elem;/**......
  • LeetCode-Java-893. Groups of Special-Equivalent Strings
    题目YouaregivenanarrayAofstrings.TwostringsSandTarespecial-equivalentifafteranynumberofmoves,S==T.Amoveconsistsofchoosingtwoindices......