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. 《java.util.concurrent 包源码阅读》07 LinkedBlockingQueue

    这篇文章来说说稍微复杂一些的LinkedBlockingQueue.LinkedBlockingQueue使用一个链表来实现,会有一个head和tail分别指向队列的开始和队列的结尾.因此Linked ...

  2. js 与 ios Android交互

    一.android 交互 1.js调用webview 在android API Level 17及以上的版本中,就会出现js调用不了android的代码,这是版本兼容的问题,需要在调用的方法上面加一个 ...

  3. apache编译安装 httpd 2.2 httpd 2.4

    #apache编译安装#httpd 2.2 , httpd 2.4 #!/bin/sh #apache编译安装 #httpd 2.2 , httpd 2.4 #centos #rpm -e httpd ...

  4. KVM克隆 快照

    原创博文安装配置KVM http://www.cnblogs.com/elvi/p/7718574.htmlweb管理kvm http://www.cnblogs.com/elvi/p/7718582 ...

  5. php中使用swoole实现头协议

    当需要有数据交互的时候,为了安全起见,双方会协商一个固定的头协议,这样必须实现其头协议才能拿到数据. PHP的swoole完美的提供了固定包头+包体协议自动分包. 首先需要通过open_length_ ...

  6. angular中复制文字到剪切板

    function copyToClipboard(oElement, value) { var aux = document.createElement("input"); if ...

  7. json数据导出excel

    最近做的一个项目涉及到把数据导出到excel里面,网上找来找去,最终找到两种办法,废话不多说了 第一种: 拿到表格的id就可以抓取表格的数据,导出到excel,这种方式简单粗暴,引入这两个插件即可. ...

  8. java学习笔记—集合之Map集合

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Times } p.p2 { margin: 0.0p ...

  9. java 中Map 使用

    Map用于保存具有映射关系的数据,Map里保存着两组数据:key和value,它们都可以使任何引用类型的数据,但key不能重复.所以通过指定的key就可以取出对应的value.Map接口定义了如下常用 ...

  10. Android Service组件在新进程绑定(bindService)过程

    1.首先看两个样例 (1)进程内 Client端 public class CounterService extends Service implements ICounterService { .. ...