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.
给定一颗二叉树,求出二叉树每一层节点的平均值,第一反应是使用二叉树的层次遍历,先回忆一下二叉树的层次遍历
void level_tree(bintree t){
Queue q;
bintree temp;
if(!t){
printf("the tree is empty\n");
return ;
}
q.add(t);
while(!q.isEmpty){
t=poll(q); //出队
printf("%c ",t.val);
if(t.left != null){
q.add(t.left);
}
if(t.right != null){
q.add(t.right);
}
}
}
 

有了模型之后,并不能直接使用,因为这一题要的是每一层的均值,我们需要记录下某一层的节点都有哪些,层次遍历时候,当开始遍历某一层时,队列的大小就是该层的节点数。

public List<Double> averageOfLevels(TreeNode root) {
List < Double > res = new ArrayList < > ();
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty())
{
int n = q.size();
double sum = 0.0;
for(int i = 0; i<n;i++) //这个地方设计的比较巧妙,用来计算一层的节点
{
TreeNode x = q.poll();
sum += x.val;
if(x.left != null)
q.add(x.left);
if(x.right != null)
q.add(x.right);//同q.offer }
res.add(sum / n);
}
return res;
}
 
 
 
 
 

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

  1. 【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 ...

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

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

  3. 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 ...

  4. 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 ...

  5. 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 of an ...

  6. [LeetCode&Python] Problem 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 ...

  7. 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 ...

  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 of an ...

  9. [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 of an ...

随机推荐

  1. 一个RtspServer的设计与实现和RTSP2.0简介

    一个RtspServer的设计与实现和RTSP2.0简介   前段时间着手实现了一个RTSP Server,能够正常实现多路RTSP流的直播播放,因项目需要,只做了对H.264和AAC编码的支持,但是 ...

  2. 迷宫问题-POJ 3984

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24348   Accepted: 14206 Descriptio ...

  3. 3255:十进制到六进制-poj

    3255:十进制到六进制 总时间限制:  1000ms 内存限制:  65536kB 描述 进制转换: 将十进制(不超过int类型表示的范围)的数转换为六进制的数. 输入 输入为第一行是组数n,后面n ...

  4. POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 49962   Accepted: 18421 Descr ...

  5. Linux磁盘分区(二):删除

    ***********************************************声明************************************************ 原创 ...

  6. dom4j详解

    Dom4j下载及使用Dom4j读写XML简介要使用dom4j读写XML文档,需要先下载dom4j包,dom4j官方网站在 http://www.dom4j.org/目前最新dom4j包下载地址:htt ...

  7. IDEA 无法运行Junit, 报错Class not found xxxx Empty test suite.

    网上搜了一圈没找到答案, 最后才发现是因为testmodule没有把class编译到主代码编译的路径.

  8. require.js模块化写法

    模块化 模块就是实现特定功能的一组方法.只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. 下述两种写法等价 exports 对象是当前模块的导出对象,用于导出模块公有方法和属性. ...

  9. Asteroids!-裸的BFS

    G - Asteroids! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  10. Android PopupWindows

    今天了解到PopupWindows这个布局,PopupWindow这个类用来实现一个弹出框,能够使用随意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的. 以下是一个实例 xml ...