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. ElasticSearch和Kibana 5.X集群的安装

    ElasticSearch和Kibana 5.X集群的安装 1.准备工作 1.1.下载安装包 1.2.系统的准备 2.ElasticSearch集群的安装 2.1.修改 config/elastics ...

  2. epoll全面讲解:从实现到应用

    多路复用的适用场合 •     当客户处理多个描述符时(例如同时处理交互式输入和网络套接口),必须使用I/O复用. •     如果一个TCP服务器既要处理监听套接口,又要处理已连接套接口,一般也要用 ...

  3. 深入理解计算机系统(1.1)------Hello World 是如何运行的

    上一篇序章我谈了谈 程序员为啥要懂底层计算机结构 ,有人赞同也有人反对,但是这并不影响 LZ 对深入理解计算机系统研究的热情.这篇博客以案例驱动的模式,通过跟踪一个简单 Hello World 程序的 ...

  4. vue非父子组件间通信

    有时候非父子关系的组件也需要通信.在简单的场景下,使用一个空的Vue实例作为中央事件总线: 有时候非父子关系的组件也需要通信.在简单的场景下,使用一个空的 Vue 实例作为中央事件总线: var bu ...

  5. linux bash 和 sh的区别

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt189 Linux 中的 shell 有很多类型,其中最常用的几种是: Bou ...

  6. JS解析JSON 注意事项总结

    0.必须先解析看看,不然看了白看   地址: http://www.bejson.com/ 1.返回的节点内是不是一个json. 如  {id:1,names:"[{name:A},{nam ...

  7. asp.net core 2.0 web api基于JWT自定义策略授权

    JWT(json web token)是一种基于json的身份验证机制,流程如下: 通过登录,来获取Token,再在之后每次请求的Header中追加Authorization为Token的凭据,服务端 ...

  8. 【2017集美大学1412软工实践_助教博客】团队作业4——第一次项目冲刺(Alpha版本)小组 成绩

    第四次团队作业成绩公布 题目 团队作业4: http://www.cnblogs.com/happyzm/p/6722264.html 团队成绩 成绩公示如下: 检查项 会议内容 代码签入 心得体会或 ...

  9. 结对作业1----基于flask框架的四则运算生成器

    011.012结对作业 coding地址:https://coding.net/u/nikochan/p/2nd_SE/git 一.作业描述 由于上次作业我没有按时完成,而且庞伊凡同学编程能力超棒,所 ...

  10. 结对作业-基于GUI的四则运算

    一.需求分析 1.题目要求: 我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序.进一步,本次要求把这个程序做成GUI(可以是Windows PC 上的,也可以是Mac.Linux,web ...