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

代码:

使用队列的先进先出的特点:

 1 class Solution {
 2 public:
 3     vector<double> averageOfLevels(TreeNode* root) {
 4         vector<double> ret;
 5         queue<TreeNode*> q;
 6         q.push(root);
 7         while (!q.empty()) {
 8             long long t = 0;
 9             int n = q.size();
10             for (int i = 0; i < n; i++) {
11                 auto node = q.front();
12                 q.pop();
13                 if (node->left) q.push(node->left);
14                 if (node->right) q.push(node->right);
15                 t += node->val;
16             }
17             ret.push_back(double(t)/double(n));
18         }
19         return ret;
20     }
21 };
时间: 2024-07-30 22:45:21

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

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

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