leetcode 637 python3 76ms 二叉树的层平均值


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

from collections import deque

class Solution:
    def averageOfLevels(self, root):
        """
        :type root: TreeNode
        :rtype: List[float]
        """
        res = []
        q = deque([root])
        while q:
            curr_sum = 0
            node_num_this_layer = len(q)
            for i in range(0, node_num_this_layer):
                node = q.popleft();
                curr_sum += node.val
                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)
            res.append(1.0*curr_sum/node_num_this_layer)
        return res

原文地址:https://www.cnblogs.com/theodoric008/p/9388281.html

时间: 2024-07-31 19:53:49

leetcode 637 python3 76ms 二叉树的层平均值的相关文章

leetcode 637 C++ 16ms 二叉树的层平均值

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ #include<queue> class Solution { public: vector<double> averageOfLevels

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位

637. 二叉树的层平均值

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7输出: [3, 14.5, 11]解释:第0层的平均值是 3, 第1层是 14.5, 第2层是 11. 因此返回 [3, 14.5, 11].注意: 节点值的范围在32位有符号整数范围内. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *le

leetCode题解之求二叉树每层的平均值

1.题目描述 Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. 计算二叉树每一层的节点的数据域的平均值. 2.题目分析 使用广度优先遍历方法,逐层对二叉树进行访问,求均值.使用一个队列数据结构完成对二叉树的访问,队列(queue)的特点是FIFO,即先进先出,queue的几种常用的方法是: queue::front()  :访问队首

力扣——二叉树的层平均值

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入:    3   / \  9  20    /  \   15   7输出: [3, 14.5, 11]解释:第0层的平均值是 3,  第1层是 14.5, 第2层是 11. 因此返回 [3, 14.5, 11]. 注意: 节点值的范围在32位有符号整数范围内. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/average-of-levels-in-binary-

LeetCode | 面试题34. 二叉树中和为某一值的路径【剑指Offer】【Python】

LeetCode 面试题34. 二叉树中和为某一值的路径[剑指Offer][Medium][Python][回溯] 问题 力扣 输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径.从树的根节点开始往下一直到叶节点所经过的节点形成一条路径. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 返回: [ [5,4,11,2], [5,8,4,5] ] 提示: 节点总数 <= 10000 注意:本题与主站 1

Leetcode 101 Symmetric Tree 二叉树

判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树 而我的做法是改下Same Tree的函数,改动的是第27行 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNo

C语言实现最大二叉树并按层遍历

题目:给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左子树是通过数组中最大值左边部分构造出的最大二叉树. 右子树是通过数组中最大值右边部分构造出的最大二叉树. 通过给定的数组构建最大二叉树,并且输出这个树的根节点. Example 1: 输入: [3,2,1,6,0,5] 输入: 返回下面这棵树的根节点: 6 / 3 5 \ / 2 0 1 分析:(1)通过审题可知通过先序创建二叉树进行创建,即创建根节点,左子树,右子数. 所以选择递归的

[Leetcode] Symmetric tree 对称二叉树

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if you could solve it both rec