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 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:
- 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的更多相关文章
- LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)
637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...
- 【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 ...
- 637. Average of Levels in Binary Tree - LeetCode
Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- git 工作流介绍
GIT Git工作流你可以理解为工作中团队成员遵守的一种代码管理方案,在Git中有以下几种工作流方案作为方案指导: 集中式工作流 功能开发工作流 Gitflow工作流 Forking工作流 下面针对性 ...
- JVM(一) OpenJDK1.8源码在Ubuntu16.04下的编译
笔者最近在学习周志明老师编写的<深入理解Java虚拟机>一书,书中第一章的实战部分就是"自己编译JDK",不过书中提到的是OpenJDK 7的编译.由于现在Java开发 ...
- python2 urllib2抓取51job网的招聘数据
#coding=utf-8 __author__ = "carry" import sys reload(sys) sys.setdefaultencoding('utf-8') ...
- py2 HTMLTestRunner报告
直接上代码吧. #coding:utf-8 #__author__ = 'carry' import unittest,HTMLTestRunner class Hello(unittest.Test ...
- webservice Dome--一个webservice的简单小实例
1.理解:webservice就是为了实现不同服务器上不同应用程序的之间的通讯 2.让我们一步一步的来做一个webservice的简单应用 1)新建一个空的web应用程序,在程序上右键,新建项目,选择 ...
- 【★】Web精彩实战之<智能迷宫>
JS精彩实战之<智能迷宫> ---宝贵编程经验分享会--- hello大家好,这里是Web云课堂,之前的一年里我们经历了Html和CSS的系统攻城,此时的你们已经是做静态(动静结 ...
- 控制结构(9) 管道(pipeline)
// 上一篇:线性化(linearization) // 下一篇:指令序列(opcode) 最近阅读了酷壳上的一篇深度好文:LINUX PID 1 和 SYSTEMD.这篇文章介绍了systemd干掉 ...
- 201521123089 《Java程序设计》第3周学习总结
1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 2. 书面作 ...
- 201521123042《Java程序》第二周总结
1. 本周学习总结 了解枚举类型的使用方法. 学会使用ArrayList替换数组,并且学会运用相关函数,例如: strList.contains(str)(判断数组中是否包含字符串str,包含则返回t ...
- 201521123045 《Java程序设计》 第十三周学习总结
201521123045 <Java程序设计> 第十三周学习总结 1. 本周学习总结 2. 书面作业 Q1.网络基础 1.1 比较ping www.baidu.com与ping cec.j ...