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相关推荐

  1. 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 ...

  2. leetcode 637. Average of Levels in Binary Tree | 637. 二叉树的层平均值(Java)

    题目 https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/ 题解 1.参考"二叉树按层打印"写的解法 ...

  3. 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 ...

  4. 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 ...

  5. C#LeetCode刷题之#110-平衡二叉树(Balanced Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4074 访问. 给定一个二叉树,判断它是否是高度平衡的二叉树. 本 ...

  6. [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)

    问题 给出一棵二叉树,找出它的最小深度. 最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数. 初始思路 不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得 ...

  7. 【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 ...

  8. C#LeetCode刷题之#226-翻转二叉树(Invert Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4080 访问. 翻转一棵二叉树. 输入: 4    /   \   ...

  9. Leetcode 部分题解

    算法思想 双指针 排序 快速选择 堆排序 桶排序 荷兰国旗问题 贪心思想 二分查找 分治 搜索 BFS DFS Backtracking 动态规划 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增 ...

最新文章

  1. 【青少年编程】陈晓光:打靶游戏
  2. 深入浅析mysql引擎
  3. bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会【树形dp】
  4. [分布式训练] 单机多卡的正确打开方式:理论基础
  5. mac系统,鼠标移动太慢
  6. Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数
  7. Oracle ORA-01555(快照过旧)
  8. 内存管理——初始化流程
  9. 从调试数据分析USB通信协议——UVC摄像头【UVC类设备】(五)
  10. 北交大计算机老师夏嘉楠,北京交通大学院系部处文件-馆档网.DOC
  11. matplotlib入门基础(五)轴坐标范围 轴刻度比例 轴刻度
  12. vue 总结一项目建立及文件夹结构配置
  13. 思维导图之《一位股票投资家的良知:我为何放弃技术分析》
  14. 10009---59条令人捧腹但真实的程序员编程语录
  15. 手机号码和姓名脱敏(加密*)
  16. mega raid linux,在lsi megaraid sas 8204elp 装linux系统(未完待续)
  17. R 语言多模型QQ plot
  18. 探寻全新模式 SaaS服务商破局
  19. 多功能智慧杆如何实现智能红绿灯应用?
  20. 使用SpringCloud构建简单的服务者和消费者

热门文章

  1. Go实现Raft第四篇:持久化和调优
  2. keyshot怎么批量渲染_怎么快速设置Keyshot渲染参数
  3. 3 RabbitMQ高级特性 3
  4. 关于一个跨域的小问题
  5. uploadify.js
  6. 多方安全计算(MPC)原理简介
  7. 一行命令轻松获取JVM默认配置
  8. Pytorch入门-2
  9. 关于redis的持久化
  10. Netty之Bootstrap详解