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 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.
这道题描述的是:
给我们一颗完全二叉树,我们求出二叉树每层的节点平均值 我的思想:
对二叉树进行广度遍历,用一个二维数组存下每一层的所有节点
再对每一层所有节点进行求平均数 伪代码:
python中数组是动态的
用一个levels列表,里面每一个元素都是一个列表,列表里存着每层的所有节点
广度遍历的时候,动态生成下一个元素和下一层列表 1 levels = [[root] ] 根自己是第一层,levels里面
2 对levels 一个一个拿出里面的列表用level表示
(如果取出来的是空列表,说明到最后一层了,跳出循环)
2.1 当前列表level为一层,里面存着所有当前层元素
2.2 为levels追加一个空列表[] 用于存储下一层
2.3 一个一个取出level里面的元素node
如果 node有left,node.left追加到下一层列表
如果 node有right,node.right追加到下一层列表
2.4 计算当前层所有节点的平均值,追加大结果列表res 我的python代码:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""
levels = [[root]] #将要进行广度遍历,每一层新开一个列表,每个列表存着每层的节点
res = [ ] #用于存储每层的平均数
i = 0
while i < len(levels) and levels[i] != []:
level = levels[i]
j = 0
temp = 0 # 临时变量用于存储当前层的节点值加和
levels.append([]) #开启新的一层
while j < len(level):
node = level[j]
if node.left is not None:
levels[i+1].append(node.left)
if node.right is not None:
levels[i+1].append(node.right)
temp+=node.val
j += 1
res.append(temp/j)
i += 1
return res
leetcode算法: Average of Levels in Binary Tree的更多相关文章
- [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 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)
637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...
- 637. Average of Levels in Binary Tree - LeetCode
Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...
- 【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 ...
- LeetCode算法题-Average of Levels in Binary Tree(Java实现)
这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...
随机推荐
- 【Unity3D与23种设计模式】中介者模式(Mediator)
GoF中定义: 定义一个接口来封装一群对象的互动行为 中介者通过移除对象之间的引用 以减少他们之间的耦合度 并且能改变它们之间的互动独立性 游戏做的越大,系统划分的也就越多 如事件系统,关卡系统,信息 ...
- Ext概述
Ext是一个具有丰富组件的javascript集合类库,除了自身提供的一套选择器.效果.ajax等功能,还提供了大量的javascript创建页面元素的类.方法.这个意味着:只要客户端支持javasc ...
- java 获取文件内所有文件名
package com.xinwen.user.controller; import java.io.File;import java.util.ArrayList;import java.util. ...
- 13.HashMap TreeMap HashTable LinkedHashMap 的区别
数据库基本连接equals和hashCode详解 http://www.cnblogs.com/XMMDMW/p/6502355.html
- linux常用命令汇总(更新中...)
文本查看与编辑 1.文本编辑命令 vi/vim 2.查看文件内容命令 命令 说明 命令格式 参数 cat 将一个文件的内容连续输出在屏幕上 cat [-option] 文件名 -n:将行号一起显示在 ...
- redis慢日志
redis的slowlog是redis用于记录记录慢查询执行时间的日志系统.由于slowlog只保存在内存中,因此slowlog的效率很高,完全不用担心会影响到redis的性能.Slowlog是Red ...
- Java高级篇(一)——线程
前面我们系统的了解了Java的基础知识,本篇开始将进入到Java更深层次的介绍,我们先来介绍一下Java中的一个重要的概念--线程. 一.什么是线程 在了解线程前,我们首先要了解进程的概念.进程是操作 ...
- linux服务器添加一块新硬盘不用重新启动机器的操作
Linux系统添加一块新硬盘不用关闭系统即可加载硬盘信息的操作 因之前换过硬盘重装系统,硬盘上的数据没有拷贝出来,开发人员问我要备份,炸了.我只好联系机房让他把之前换掉的硬盘插回服务器.但是插好之后f ...
- 数据库 --> MySQL使用
MySQL使用 代码: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>#includ ...
- 用jQuery.delegate()将事件绑定在父元素上面
1.先看看官方的示例: <html> <head> <script type="text/javascript" src="/jquery/ ...