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:

  1. 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) {
//存放平均值的list
List<Double> list = new ArrayList<>();
//存放树的队列,用于层次遍历。
Queue<TreeNode> queue = new ArrayDeque<>();
// 存放对应树的层次信息,便于计算均值。
Queue<Integer> queue1 = new ArrayDeque<>();
// 初始层级h=0
int h = 0;
// 对应层级的节点数
int n = 0;
// 对应层级的节点数之和,要用double型。
double sum = 0; queue.offer(root);
// 初始层级放入后+1,根节点只有一个。
queue1.offer(new Integer(h++));
while(!queue.isEmpty()){
root = queue.poll();
int hh = queue1.poll().intValue();
// 如果处于同一层,需要将层级+1,并完成平均数计算,放入list
if(h == hh){
h++;
list.add(new Double(sum/n));
sum = 0;
n = 0;
}
sum += root.val;
n++;
// 层次遍历,放入左右子树和对应层级
if(root.left != null){
queue.offer(root.left);
queue1.offer(new Integer(h));
}
if(root.right != null){
queue.offer(root.right);
queue1.offer(new Integer(h));
}
}
// 最后一次的均值未计算,要单独处理
list.add(new Double(sum/n));
return list;
}
}

Average of Levels in Binary Tree的更多相关文章

  1. LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)

    637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...

  2. 【Leetcode_easy】637. Average of Levels in Binary Tree

    problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...

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

    Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...

  4. leetcode算法: Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  5. 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  6. [LeetCode] Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  7. [Swift]LeetCode637. 二叉树的层平均值 | Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  8. LeetCode 637 Average of Levels in Binary Tree 解题报告

    题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  9. LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)

    题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o ...

随机推荐

  1. exjs上传图片异常:com.jspsmart.upload.SmartUploadException: File can't be saved (1120).

    错误: 文件名格式不对:未命??.jpg SmartUpload mySmartUpload = new SmartUpload(); com.jspsmart.upload.File myFile ...

  2. Spring Cloud官方文档中文版-服务发现:Eureka服务端

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR3/#spring-cloud-eureka-server 文中例子我做了一些 ...

  3. 栈的实现Java

    package practice; import java.util.Iterator; //栈 public class MyStack<T> implements Iterable&l ...

  4. 第3阶段——内核启动分析之start_kernel初始化函数(5)

    内核启动分析之start_kernel初始化函数(init/main.c) stext函数启动内核后,就开始进入start_kernel初始化各个函数, 下面只是浅尝辄止的描述一下函数的功能,很多函数 ...

  5. yum的初步了解与使用

    什么是yum Yum(Yellow dog Updater,Modified)是一个基于RPM包管理的字符前端软件包管理器.能够从指定的服务器自动下载RPM包并且安装,可解决软件包相关依赖性,并且一次 ...

  6. CCNA+NP学习笔记—交换网络篇

    本章关于企业网络的最底层--交换层,难度较低,主要为以后三层的路由做铺垫.所有笔记的分类顺序为:序章→交换层→路由层→运营商,体现了从企业网到互联网的学习顺序. 注:思科设备命令行通常不分大小写!以后 ...

  7. 201521123051《Java程序设计》第六周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...

  8. 201521123050 《Java程序设计》第5周学习总结

    1. 本周学习总结 2. 书面作业 1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误.并分析输出结果. 答:不 ...

  9. 201521123037 《Java程序设计》第1周学习总结

    #1.本章学习总结 了解java的发展历程 能够区分JVM.JRE.JDK之间的关系 了解文件名.类名要相同,且文件名大小写代表不同的名称 初步了解java编程语句 学会用控制台编译java程序 #2 ...

  10. 让你的python程序同时兼容python2和python3

    python邮件列表里有人发表言论说「python3在10内都无法普及」.在我看来这样的观点有些过于悲观,python3和python2虽然不兼容,但他们之间差别并没很多人想像的那么大.你只需要对自己 ...