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

Example 1:

  1. Input:
  2. 3
  3. / \
  4. 9 20
  5. / \
  6. 15 7
  7. Output: [3, 14.5, 11]
  8. Explanation:
  9. 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.

思路:首先要使用层次遍历,因为每次遍历后要计算对应层的平均值。所以又需要加上对层次的标记。

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public List<Double> averageOfLevels(TreeNode root) {
  12. //存放平均值的list
  13. List<Double> list = new ArrayList<>();
  14. //存放树的队列,用于层次遍历。
  15. Queue<TreeNode> queue = new ArrayDeque<>();
  16. // 存放对应树的层次信息,便于计算均值。
  17. Queue<Integer> queue1 = new ArrayDeque<>();
  18. // 初始层级h=0
  19. int h = 0;
  20. // 对应层级的节点数
  21. int n = 0;
  22. // 对应层级的节点数之和,要用double型。
  23. double sum = 0;
  24.  
  25. queue.offer(root);
  26. // 初始层级放入后+1,根节点只有一个。
  27. queue1.offer(new Integer(h++));
  28. while(!queue.isEmpty()){
  29. root = queue.poll();
  30. int hh = queue1.poll().intValue();
  31. // 如果处于同一层,需要将层级+1,并完成平均数计算,放入list
  32. if(h == hh){
  33. h++;
  34. list.add(new Double(sum/n));
  35. sum = 0;
  36. n = 0;
  37. }
  38. sum += root.val;
  39. n++;
  40. // 层次遍历,放入左右子树和对应层级
  41. if(root.left != null){
  42. queue.offer(root.left);
  43. queue1.offer(new Integer(h));
  44. }
  45. if(root.right != null){
  46. queue.offer(root.right);
  47. queue1.offer(new Integer(h));
  48. }
  49. }
  50. // 最后一次的均值未计算,要单独处理
  51. list.add(new Double(sum/n));
  52. return list;
  53. }
  54. }

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. git 工作流介绍

    GIT Git工作流你可以理解为工作中团队成员遵守的一种代码管理方案,在Git中有以下几种工作流方案作为方案指导: 集中式工作流 功能开发工作流 Gitflow工作流 Forking工作流 下面针对性 ...

  2. JVM(一) OpenJDK1.8源码在Ubuntu16.04下的编译

    笔者最近在学习周志明老师编写的<深入理解Java虚拟机>一书,书中第一章的实战部分就是"自己编译JDK",不过书中提到的是OpenJDK 7的编译.由于现在Java开发 ...

  3. python2 urllib2抓取51job网的招聘数据

    #coding=utf-8 __author__ = "carry" import sys reload(sys) sys.setdefaultencoding('utf-8') ...

  4. py2 HTMLTestRunner报告

    直接上代码吧. #coding:utf-8 #__author__ = 'carry' import unittest,HTMLTestRunner class Hello(unittest.Test ...

  5. webservice Dome--一个webservice的简单小实例

    1.理解:webservice就是为了实现不同服务器上不同应用程序的之间的通讯 2.让我们一步一步的来做一个webservice的简单应用 1)新建一个空的web应用程序,在程序上右键,新建项目,选择 ...

  6. 【★】Web精彩实战之<智能迷宫>

    JS精彩实战之<智能迷宫>      ---宝贵编程经验分享会--- hello大家好,这里是Web云课堂,之前的一年里我们经历了Html和CSS的系统攻城,此时的你们已经是做静态(动静结 ...

  7. 控制结构(9) 管道(pipeline)

    // 上一篇:线性化(linearization) // 下一篇:指令序列(opcode) 最近阅读了酷壳上的一篇深度好文:LINUX PID 1 和 SYSTEMD.这篇文章介绍了systemd干掉 ...

  8. 201521123089 《Java程序设计》第3周学习总结

    1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 2. 书面作 ...

  9. 201521123042《Java程序》第二周总结

    1. 本周学习总结 了解枚举类型的使用方法. 学会使用ArrayList替换数组,并且学会运用相关函数,例如: strList.contains(str)(判断数组中是否包含字符串str,包含则返回t ...

  10. 201521123045 《Java程序设计》 第十三周学习总结

    201521123045 <Java程序设计> 第十三周学习总结 1. 本周学习总结 2. 书面作业 Q1.网络基础 1.1 比较ping www.baidu.com与ping cec.j ...