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   7Output: [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代码:
 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def averageOfLevels(self, root):
10         """
11         :type root: TreeNode
12         :rtype: List[float]
13         """
14         levels = [[root]] #将要进行广度遍历,每一层新开一个列表,每个列表存着每层的节点
15         res = [ ]  #用于存储每层的平均数
16         i = 0
17         while i < len(levels) and levels[i] != []:
18             level = levels[i]
19             j = 0
20             temp = 0    # 临时变量用于存储当前层的节点值加和
21             levels.append([])   #开启新的一层
22             while j < len(level):
23                 node = level[j]
24                 if node.left is not None:
25                     levels[i+1].append(node.left)
26                 if node.right is not None:
27                     levels[i+1].append(node.right)
28                 temp+=node.val
29                 j += 1
30             res.append(temp/j)
31             i += 1
32         return res
				
时间: 2024-10-20 11:03:16

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

LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)

637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目描述 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / 9 20 / 15 7 输出: [3, 14.5, 11] 解释: 第 0 层的平均值是 3,第 1 层是 14.5,第 2 层是 11.因此返回 [3, 14.5, 11]. 注意: 节点值的范围在32位

LeetCode算法题-Average of Levels in Binary Tree(Java实现)

这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平均值.例如: 3 / 9 20 / 15 7 输出:[3,14.5,11] 说明:第一层上的节点的平均值为3,第二层上的节点的平均值为14.5,第三层上的节点的平均值为11.因此返回[3,14.5,11]. 注意:节点值的范围在32位有符号整数的范围内. 本次解题使用的开发工具是eclipse,jd

[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 le

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 le

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 le

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 le

Average of Levels in Binary Tree

这道题被标记为简单,学过广搜(BFS)应该会很容易做出来 思路: BFS算法搜索每一层的节点,并把他们保存在一个列表a中,再利用循环将这一层的total加起来,并判断当前节点是否有子节点,有就添加到列表的后面,当搜索完一层的节点,就计算每一层的平均值并添加到另外一个新的列表b中,然后再判断是否列表为空,为空就返回列表b,否则就再次循环剩下的. 代码: 1 class Solution(object): 2 def averageOfLevels(self, root): 3 ""&qu

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig