【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 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.
解析
给一颗树,求每一层的平均数,返回一个List
思路
我的想法是将一层的所有节点加入到一个list中,计算list的平均数,并递归计算list中节点的子节点(会构建多个list,空间利用较多)
最优解是使用了广度优先算法,利用queue将一层的节点先全部入列,计算平均数,并将其子节点入列,每层循环用n记录当前queue中的节点数,作为内层循环的循环次数
我的解法
public List<Double> averageOfLevels(TreeNode root) {
List<Double> avg = new ArrayList<>();
if (root == null) {
return null;
}
List<TreeNode> list = new ArrayList<TreeNode>() {
{
add(root);
}
};
getAvg(list, avg);
return avg;
}
private void getAvg(List<TreeNode> list, List<Double> avg) {
if (list == null || list.size() <= 0) {
return;
}
List<TreeNode> childTreeNodeList = new ArrayList<>();
Double sum = 0D;
for (TreeNode t : list) {
sum += t.val;
if (t.left != null) {
childTreeNodeList.add(t.left);
}
if (t.right != null) {
childTreeNodeList.add(t.right);
}
}
avg.add(sum / list.size());
getAvg(childTreeNodeList, avg);
}
最优解
public List<Double> averageOfLevelsBFS(TreeNode root) {
if (root == null) {
return null;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>() {
{
add(root);
}
};
List<Double> avg = new ArrayList<>();
while (!queue.isEmpty()) {
//queue的长度为当前行的元素数
int n = queue.size();
Double sum = 0D;
for (int i = 0; i < n; i++) {
TreeNode t = queue.poll();
sum += t.val;
if (t.left != null) {
queue.offer(t.left);
}
if (t.right != null) {
queue.offer(t.right);
}
}
avg.add(sum / n);
}
return avg;
}
【leetcode】637. Average of Levels in Binary Tree的更多相关文章
- 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 【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] 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 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 ...
- 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&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 ...
- 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 ...
随机推荐
- Navicat安装及使用
一.安装Navicat 1.下载安装文件:navicat11.0.17_premium_cs_x86.exe(32位) 2.Oracle 的 Instance Client:instantclient ...
- 《剑指offer》数学题及其它 (牛客11.05)
比较多的思维题,涉及位运算.快速幂.二进制.约瑟夫问题.队列.贪心.dp等等. 难度 题目 知识点 ☆ 12.数值的整数次方 细节,快速幂 ☆☆ 47.求1+2+3+···+n 思维发散 ☆☆ 48. ...
- 前端接收 post 请求返回的文件
坐标过多无法用Get请求,只能用post下载. 但发现ajax发送的post请求没有触发下载,返回的流媒体会存在于接口返回的response中. 查询发现AJAX并不会唤起浏览器的下载窗口,AJAX设 ...
- 最新 中钢网java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.中钢网等10家互联网公司的校招Offer,因为某些自身原因最终选择了中钢网.6.7月主要是做系统复习.项目复盘.LeetCo ...
- Memcached stats命令及核心参数
一.stats命令 用来查看服务器的运行状态和内部数据,其中核心的参数有: 1.缓存命中率相关参数: cmd_get:总查询次数 get_hits:命中次数 get_misses:未命中次数 2.使用 ...
- jmeter边界提取器实现数据依赖
前言:没用cookie管理器去管理cookie是因为它自动匹配的cookie不对 1.已登录接口为案例,查询接口需要依赖登录后返回的cookie 2.首先调登录接口 可以看到 响应头里面返回了一串co ...
- vue-cli2和vue-cli3同时存在
https://blog.csdn.net/Jioho_chen/article/details/90455778 win下 vue-cli2 和 vue-cli3 并存,一起使用开局一张图,内容慢慢 ...
- opencv轮廓外接矩形
1.寻找轮廓 api void cv::findContours( InputOutputArray image, OutputArrayOfArrays contours, OutputArray ...
- Asp.Net Core集成Swagger
工作中一个公司会有很多个项目,项目之间的交互经常需要编写 API 来实现,但是编写文档是一件繁琐耗时的工作,并且随着 API 的迭代,每次都需要去更新维护接口文档,很多时候由于忘记或者人员交替的愿意造 ...
- Ctrl+Tab
很好用的快捷键. 可以在浏览器中自由切换,也可以在编辑器中自由切换.