637. Average of Levels in Binary Tree
来源:互联网 发布:如何做一名程序员 编辑:程序博客网 时间:2024/05/16 01:29
1.题目
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].
2.分析
这道题看提交量与Accept的比例,也知道是一道难度相对较为容易的题,树形结构,求层次均值,我想到的是层次遍历,记录每一层的数值总和与每一层的数个数,这样就能每一层求出均值。而看了其他人解题,有用深度搜索,也广度搜索,但是思想是一样的,就是记录每一层的总和与计数值,最后求平均值,只是深度/广度搜索中采用递归,代码简洁不少,我开了两个栈的辅助空间,其实栈的特性本身也就是实现递归的一种手段。
3.解题
我的解题:
public class Solution {
public List averageOfLevels(TreeNode root) {
ArrayList<Double>list = new ArrayList<Double>(); // 边界处理 if(root==null){ return list; } Stack<TreeNode>one = new Stack<TreeNode>(); Stack<TreeNode>two = new Stack<TreeNode>(); one.push(root); while(!one.isEmpty()||!two.isEmpty()){ long sum = 0; // int求和可能会溢出,而long32位完全满足int的求和,而且系统自动进行类型转换 int count = 0; int flag = 0; // 标志是栈是否进行遍历求和 while(!one.isEmpty()){ flag = 1; TreeNode tem = one.pop(); sum += tem.val; count++; if(tem.left!=null){ two.push(tem.left); } if(tem.right!=null){ two.push(tem.right); } } if(flag==1) list.add((double)sum/count); sum = 0; count = 0; flag = 0; while(!two.isEmpty()){ flag = 1; TreeNode tem = two.pop(); sum += tem.val; count++; if(tem.left!=null){ one.push(tem.left); } if(tem.right!=null){ one.push(tem.right); } } if(flag==1) list.add((double)sum/count); } return list;}
}
别人解题:
public List averageOfLevels(TreeNode root) {
List result = new ArrayList<>();
Queue q = new LinkedList<>();
if(root == null) return result;q.add(root);while(!q.isEmpty()) { 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); } result.add(sum / n);}return result;
}
4.总结
别人好像从一开始就直接用Double类进行数值操作,而我- -,因为输入是int,所以直接对int类型值进行操作,最后存储的时候,强转为double类型,这就有问题了,int求和溢出,所以我最后将使用long保存最后求和值,long的字段长度与double的是一样的,完全满足int求和。所以还是解题之前想清楚,同时代码要规范,低级错误避免,不然很多时候,思路没有问题就是A不了。
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- [Leetcode] Binary tree-- 637. Average of Levels in Binary Tree
- 基于光流的视频语义分割和物体检测
- Eclipse-EE查看classes文件
- 删除数组的常见几种用法
- 解决cannot resolve symbol xxx
- Java内存分析
- 637. Average of Levels in Binary Tree
- Reduce运行到99.99%到100%,最后出现了错误,导致任务失败
- 【BIM】Autodesk Navisworks安装成功后,Manage打不开怎么办?
- Java拾遗-编程风格
- 剑指offer:反转链表
- qt信号槽接收不到的情况(自定义数据类型+多线程)
- CSS常见布局效果
- 基于主键一对一关联映射--单双向
- 从无头单链表中删除节点