Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

Example 1:

<b>Input:</b>
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:

The range of node’s value is in the range of 32-bit signed integer.

分析

树的层序遍历

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> res;
double sum=0,cnt=0;
queue<TreeNode*> q;
q.push(root);
TreeNode* last=root;
TreeNode* tail;
while(!q.empty()){
TreeNode* t=q.front();
q.pop();
sum+=t->val;
cnt++;
if(t->left!=NULL){
q.push(t->left);
tail=t->left;
}
if(t->right!=NULL){
q.push(t->right);
tail=t->right;
}
if(t==last){
last=tail;
res.push_back(sum/cnt);
sum=cnt=0;
}
}
return res;
}
};

LeetCode 637. Average of Levels in Binary Tree(层序遍历)的更多相关文章

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

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

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

    题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o ...

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

  5. 637. Average of Levels in Binary Tree - LeetCode

    Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...

  6. 【Leetcode_easy】637. Average of Levels in Binary Tree

    problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...

  7. 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  8. [LeetCode&Python] Problem 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 ...

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

随机推荐

  1. [已读]JavaScript DOM编程艺术

    看到过很多人将它作为推荐入门书籍,当时我刚看完ppk和javascript精粹,于是看到这本就觉得很一般了.怎么说,它适合基础.

  2. Spark Mllib里如何将数值特征字段用StandardScaler进行标准化(图文详解)

    不多说,直接上干货! 首先,要明白为什么有时候,数值特征字段需要进行标准化? 答:因为,当我们若用回归分析算法时,必须将数值特征字段进行标准化,这是因为数值特征字段单位不同,数字差异很大,所以无法彼此 ...

  3. nginx 日志 log_format 及字段说明

    1.log_format 普通格式 log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$stat ...

  4. 编写可执行程序,其它程序调用,并返回数据,C#

    有时候在创建临时文件或文件夹,使用完成后,释放失败,删除提示占用,又不能结束主程序,就可以通过别的方法来解决 比如,另外创建一个程序,单独执行任务,完成后结束程序,并返回执行结果,上述问题就能解决. ...

  5. git命令收集

    $ git clone ... $ git status 查看状态 $ git commit -am "XXX" 提交信息 $ git commit -am "XXXX& ...

  6. top 进程管理

    top 动态查看进程 前五行解释: 第一行参数说明: top - 07:06:19    当前时间 up 10 min,  系统运行时间,格式为时:分 1 user,  当前登录用户数 load av ...

  7. C#实现MD5WITHRSA签名

    这是很久以前的写的一篇博客了,今天把他重新找出来整理一下发布到博客园 当时对接银联的时候搞了很久都没搞出来,后来一个偶然的机会发现类似的一个代码参考了一下终于弄好了 这段代码主要是实现了C#服务端对接 ...

  8. uvm_reg_defines——寄存器模型(四)

    文件: src/marcos/uvm_reg_defines 类: 无 该文件是寄存器模型src/reg/* 文件对于的宏文件,主要定义了寄存器地址位宽,寄存器数据位宽,字节的大小.计算机从最初的8, ...

  9. nconf修改密码

    修改nconf登录界面密码 [root@Cnyunwei config]# vi .file_accounts.php <?php/*## User/Password file for simp ...

  10. UWP中获取Encoding.Default

    Encoding.GetEncoding(0); 即可