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

注意:

  1. 节点值的范围在32位有符号整数范围内。

Java 实现

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> result = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return result;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            double avg = 0.0;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                if (queue.peek().left != null) {
                    queue.offer(queue.peek().left);
                }
                if (queue.peek().right != null) {
                    queue.offer(queue.peek().right);
                }
                avg += queue.poll().val;
            }
            result.add(avg / size);
        }
        return result;
    }
}

相似题目

参考资料

原文地址:https://www.cnblogs.com/hglibin/p/10849802.html

时间: 2024-11-10 12:07:09

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

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

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

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

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

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

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 le

Average of Levels in Binary Tree

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