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:

  1. The range of node's value is in the range of 32-bit signed integer.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
"""
if not root:
return None
else:
ans=[] que=[root] while que:
n=len(que)
sum=0
for i in range(n):
r=que.pop(0)
sum+=r.val
if r.left is not None:
que.append(r.left)
if r.right is not None:
que.append(r.right)
ans.append(sum/n) return ans

  

[LeetCode&Python] Problem 637. Average of Levels in Binary Tree的更多相关文章

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

  2. 637. Average of Levels in Binary Tree - LeetCode

    Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...

  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 of an ...

  4. 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

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

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

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

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

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

随机推荐

  1. string_01

    内存释放和越界 越界:(1).复制越界,(2).取值越界 构造函数 (1).默认 空(无参) (2).拷贝(const string &) (3).带参数: const char * // Z ...

  2. [原][osg][gdal]两种方式修改tiff高程

    因为对于globalmap不熟悉,不怎么怎么修改高程,好像也没有这功能. 干脆自己手动修改了高程图tiff了 由于自身一直使用osg的 自己使用了osgDB直接读取tiff,修改后保存的. 同事小周一 ...

  3. 安装gcc4.8.5

    安装gcc4.8.51. g++ --version, 4.4.7不支持c++112. 升级gcc-c++, 下载gcc    https://gcc.gnu.org/ 官网,镜像下载地址https: ...

  4. Python map/reduce

    2017-07-31 18:20:59 一.map函数 map():会根据提供的函数对指定序列做映射.第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 ...

  5. Android ListView常见配置说明

    ListView是我们经常使用的控件,但是使用中却因为各种原因无法设置出我们需要的效果,现将常用的设置记录下来方便以后查询. 1.拖动时背景变黑 android:cacheColorHint=&quo ...

  6. Android之封装好的异步网络请求框架

    1.简介  Android中网络请求一般使用Apache HTTP Client或者采用HttpURLConnection,但是直接使用这两个类库需要写大量的代码才能完成网络post和get请求,而使 ...

  7. Jersey 2.x 分支 Java SE 兼容性

    直到 Jersey 2.6 版本,Jersey 将会兼容 Java SE 6.这个情况将会在在 Jersey 2.7 的版本有所改变. 直到 Jersey 版本 2.25x, 所有的 Jersey 的 ...

  8. President's Path CodeForces - 416E (最短路,计数)

    大意: 给定无向图, 求任意两点间所有最短路经过的边数 刚开始想先用floyd跑出最短路, 然后在DAG上DP, 交了1发, 发现会算重复 贴一下题解的做法 #include <iostream ...

  9. Java数组常用API

    java.util.Arrays Arrays.asList() 数组转换成列表 String[] strArray = {"zhang", "xue", &q ...

  10. @ConditionalOnProperty 详解

    @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @Documented @Con ...