Leetcode PHP题解--D56 637. Average of Levels in Binary Tree
2019独角兽企业重金招聘Python工程师标准>>>
D56 637. Average of Levels in Binary Tree
题目链接
637. Average of Levels in Binary Tree
题目分析
返回每一层的平均值。
思路
和前一篇相似。先保存每一层的值,再逐层计算平均值即可。
最终代码
<?php
/*** Definition for a binary tree node.* class TreeNode {* public $val = null;* public $left = null;* public $right = null;* function __construct($value) { $this->val = $value; }* }*/
class Solution {/*** @param TreeNode $root* @return Float[]*/public $level = 0;public $values = [];function averageOfLevels($root) {$this->levelValues($root);$avg = [];foreach($this->values as $values){$avg[] = array_sum($values)/count($values);}return $avg;}function levelValues($root){if(is_null($root)){return $this->values;} if(!isset($this->values[$this->level])){$this->values[$this->level] = [];}$this->values[$this->level][] = $root->val;if($root->left){$this->level++;$this->averageOfLevels($root->left);$this->level--;}if($root->right){$this->level++;$this->averageOfLevels($root->right);$this->level--;}return $this->values;}
}
若觉得本文章对你有用,欢迎用爱发电资助。
转载于:https://my.oschina.net/u/2246923/blog/3047281
Leetcode PHP题解--D56 637. Average of Levels in Binary Tree相关推荐
- LeetCode - Easy - 637. Average of Levels in Binary Tree
Topic Tree Description https://leetcode.com/problems/average-of-levels-in-binary-tree/ Given the roo ...
- leetcode 637. Average of Levels in Binary Tree | 637. 二叉树的层平均值(Java)
题目 https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/ 题解 1.参考"二叉树按层打印"写的解法 ...
- 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 o ...
- 637. Average of Levels in Binary Tree
1.问题描述 Given a non-empty binary tree, return the average value of the nodes on each level in the for ...
- C#LeetCode刷题之#110-平衡二叉树(Balanced Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4074 访问. 给定一个二叉树,判断它是否是高度平衡的二叉树. 本 ...
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
问题 给出一棵二叉树,找出它的最小深度. 最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数. 初始思路 不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得 ...
- 【LeetCode从零单排】No104 Maximum Depth of Binary Tree
题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- C#LeetCode刷题之#226-翻转二叉树(Invert Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4080 访问. 翻转一棵二叉树. 输入: 4 / \ ...
- Leetcode 部分题解
算法思想 双指针 排序 快速选择 堆排序 桶排序 荷兰国旗问题 贪心思想 二分查找 分治 搜索 BFS DFS Backtracking 动态规划 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增 ...
最新文章
- 【青少年编程】陈晓光:打靶游戏
- 深入浅析mysql引擎
- bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会【树形dp】
- [分布式训练] 单机多卡的正确打开方式:理论基础
- mac系统,鼠标移动太慢
- Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数
- Oracle ORA-01555(快照过旧)
- 内存管理——初始化流程
- 从调试数据分析USB通信协议——UVC摄像头【UVC类设备】(五)
- 北交大计算机老师夏嘉楠,北京交通大学院系部处文件-馆档网.DOC
- matplotlib入门基础(五)轴坐标范围 轴刻度比例 轴刻度
- vue 总结一项目建立及文件夹结构配置
- 思维导图之《一位股票投资家的良知:我为何放弃技术分析》
- 10009---59条令人捧腹但真实的程序员编程语录
- 手机号码和姓名脱敏(加密*)
- mega raid linux,在lsi megaraid sas 8204elp 装linux系统(未完待续)
- R 语言多模型QQ plot
- 探寻全新模式 SaaS服务商破局
- 多功能智慧杆如何实现智能红绿灯应用?
- 使用SpringCloud构建简单的服务者和消费者