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. DES对称加密算法简析

    1 对称加密算法 在了解DES算法前,先加单介绍一下对称加密算法,因为DES属于对称加密算法的一种. 对称加密算法是应用较早的加密算法,技术成熟.在对称加密算法中,数据发信方将明文(原始数据)和加密密 ...

  2. smali语法(二)

    一.smali的包中信息 .class public Lcom/aaaaa; .super Lcom/bbbbb; .source "ccccc.java" 1.它是com.aaa ...

  3. windows配置git

    每次要使用git指令的时候都要去打开git bash 操作,太麻烦,要想直接在dos窗口下使用git指令需要再进行如下环境变量配置. 1.系统环境变量path添加:D://programFiles/g ...

  4. codeforces 895A Pizza Separation 枚举

    codeforces 895A Pizza Separation 题目大意: 分成两大部分,使得这两部分的差值最小(注意是圆形,首尾相连) 思路: 分割出来的部分是连续的,开二倍枚举. 注意不要看成0 ...

  5. SpringCloud Feign使用详解

    添加依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  6. GET方式提交中文编码问题以及三种解决方式

    GET方式提交在WEB中是非常常用的方式,有时候我们在使用GET方式提交请求不得不提交中文,但是TOMCAT等容器对于GET方式的编码问题总是让人折腾. 先说说流程吧: 我们的内容使用GET方式发送, ...

  7. Scala入门系列(十一):模式匹配

    引言 模式匹配是Scala中非常有特色,非常强大的一种功能. 类似于Java中的switch case语法,但是模式匹配的功能要比它强大得多,switch只能对值进行匹配,但是Scala的模式匹配除了 ...

  8. Python爬虫(十九)_动态HTML介绍

    JavaScript JavaScript是网络上最常用也是支持者对多的客户端脚本语言.它可以收集用户的跟踪数据,不需要重载页面直接提交表单,在页面嵌入多媒体文件,甚至运行网页游戏. 我们可以在网页源 ...

  9. Linux 账号管理与 ACL 权限配置

    要登陆 Linux 系统一定要有账号与口令才行,否则怎么登陆,您说是吧?不过, 不同的使用者应该要拥有不同的权限才行吧?我们还可以透过 user/group 的特殊权限配置, 来规范出不同的群组开发项 ...

  10. java.sql.SQLException: Can not issue data manipulation statements with executeQuery().

    1.错误描写叙述 java.sql.SQLException: Can not issue data manipulation statements with executeQuery(). at c ...