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 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.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> retList = new ArrayList<Double>();
        if (root == null)
            return retList;

        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.offer(root);

        while ( ! q.isEmpty() ) {
            List<TreeNode> tl = new ArrayList<TreeNode>();
            double sum = 0;
            while ( ! q.isEmpty() ) {
                TreeNode curr = q.poll();
                tl.add(curr);
                sum += (double) curr.val;
            }
            retList.add(sum / tl.size());
            for (TreeNode n : tl) {
                if (n.left != null)
                    q.offer(n.left);
                if (n.right != null)
                    q.offer(n.right);
            }
        }
        return retList;
    }
}
时间: 2024-11-04 04:08:32

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

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)

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

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

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

Average of Levels in Binary Tree

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

[LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level