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 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].
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
BFS写不熟
[一句话思路]:
(3先生)先加头、先判Empty、先取长度
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 没概念:BFS中的for循环是针对当前这一层的操作,add的元素都属于下一层
- 二叉树层遍历,q中存的是节点,记住就行了
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
BFS里既然取了长度,就用在for循环中
[复杂度]:Time complexity: O(n) Space complexity: O(n)
所有点放进去一次,拿出来一次,最终还是n
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
q.offer(root);
//isEmpty()
while (! q.isEmpty()) {
//get length
int n = q.size();
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* 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) {
//corner case
List<Double> ans = new ArrayList<Double>();
Queue<TreeNode> q = new LinkedList<TreeNode>(); if (root == null) {
return ans;
}
//bfs
//offer root
q.offer(root);
//isEmpty()
while (! q.isEmpty()) {
//get length
int n = q.size();
double sum = 0.0;
for (int i = 0; i < n; i++) {
TreeNode node = q.poll();
sum += node.val;
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
}
ans.add(sum / n);
}
//return
return ans;
}
}
637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值的更多相关文章
- 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 ...
- [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 ...
- 【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保存每层的元素个数和所有元素的和,遍历这 ...
- 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 ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 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 ...
随机推荐
- java少包汇总
1.在下载使用javax.mail的jar包时候,注意: 有的jar没有包含sun的实现,只包含了api,这类jar名称通常为javax.mail-api-x.x.x.jar,在使用smtp协议发邮件 ...
- POJ 3276 Face The Right Way(反转)
Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6038 Accepted: 2 ...
- 判断和调整library cache,data dictionary cache,buffer cache性能
Oracle SGA是oracle的内存结构,存放着oracle通过oracle进程读写的内存数据.sga分为好多组件,比如shared pool,buffer cache,redo log buff ...
- Window下MySql 5.6 安装后内存占用很高的问题
Window下MySql 5.6 安装后内存占用很高的问题 刚刚准备玩一把mysql,初学者 环境是window 7和window sever 2008, mysql是最新的5.6, 发现的问题是安装 ...
- 从一个开发的角度看负载均衡和LVS--FullNat
从一个开发的角度看负载均衡和LVS 在大规模互联网应用中,负载均衡设备是必不可少的一个节点,源于互联网应用的高并发和大流量的冲击压力,我们通常会在服务端部署多个无状态的应用服务器和若干有状态的存储服务 ...
- cinder-backup详细介绍
首先介绍Snapshot snapshot可以为volume创建快照,快照中保存了volume当前的状态,此后可以通过snapshot回溯 主要采用了Copy On Write算法.进行快照时,不牵涉 ...
- [OpenCV Qt教程] 在Qt图形界面中显示OpenCV图像的OpenGL Widget(第二部分)
本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-opengl-widget-per-visualizzare-imm ...
- C投票系统
- 基本SQL命令 (1.SQL命令使用规则/2.库管理/3.表管理/4.表记录管理/5.更改库,库的默认字符集/6.连接数据库的过程/7.数据类型)
1.SQL命令的使用规则 1.每条命令必须以 ; 结尾 2.SQL命令不区分字母大小写 3.使用 \c 终止SQL命令的执行 2.库的管理 1.库的基本操作 ...
- 转 CentOS下面安装RVM+ruby+Rails
CentOS6.2下面安装RVM+ruby+Rails (1)RVM官方网站应该是改版过一次, 使用 curl -L https://get.rvm.io | bash -s stable 下载并安装 ...