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 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:
- 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) {
getAverage(root, res, );
for(int i = ; i < res.size(); ++i){
res[i] /= nums[i];
}
return res;
}
void getAverage(TreeNode* root, vector<double> &res, int level){
if(root == nullptr) return;
if(level >= res.size()){
res.push_back();
nums.push_back();
}
res[level] += root->val;
nums[level]++;
getAverage(root->left, res, level+);
getAverage(root->right, res, level+);
}
private:
vector<double> res;
vector<int> nums;
};
LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)的更多相关文章
- [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 ...
- [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 ...
- Leetcode637.Average of Levels in Binary Tree二叉树的层平均值
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. class Solution { public: vector<double> averageOfLevels(TreeNode ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 637. Average of Levels in Binary Tree - LeetCode
Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...
- 【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 ...
随机推荐
- 【转】给网站添加X-UA-Compatible标签
X-UA-Compatible是神马? X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中 ...
- day2-作业及答案
作业:第一组: 1.接收用户输入一个年份,判断是否是闰年(判断闰年的方法是该年能被4整除并且不能被100整除,或者是可以被400整除) 2.接收用户输入一组整数,输入负数时结束输入,输出这组数字的和: ...
- C++ 虚函数的使用
虚函数是C++中用于实现多态(polymorphism)的机制.核心理念就是通过基类访问派生类定义的函数.假设我们有下面的类层次: #include <iostream> using na ...
- Ubuntu双系统无法挂载Windows10 硬盘的解决方法
我的电脑是在Windows 10下安装的Ubuntu 14.04双系统,今天进入Ubuntu系统访问Windows 10 磁盘,出现如下错误: Error mounting /dev/sda1 at ...
- kubernetes 禁用虚拟内存 swapoff -a ----- 顺便复习sed 命令
1.如果不关闭swap,就会在kubeadm初始化Kubernetes的时候报错,如下图: [ERROR Swap]: running with swap on is not supported. P ...
- iOS11 Xcode 9 按住command 单击 恢复到从前(直接跳转到定义)
iOS11 Xcode 9 按住command 单击 恢复到从前(直接跳转到定义) 2017年9月20日,苹果如期推送 Xcode 9 和 iOS 11的更新. Xcode 9正式版与之前bet ...
- vmware共享文件夹
环境: VMware Workstation 11.0 虚拟机中的系统:Ubuntu 16.04 物理机:window 7 安装好vmware tools后在 /mnt/hgfs 里没有东西,是空白的 ...
- NYOJ 35 表达式求值
一个模板了 哈哈. 这题由于已经包括了整形.浮点形了,以后也不须要特别处理了. /* 这里主要是逆波兰式的实现,使用两个stack 这里用字符串来模拟一个stack,第一步,将中缀表达式转变为后缀表达 ...
- 课程设计个人报告——基于ARM实验箱的Android交友软件的设计与实现
个人贡献 熟悉试验箱各元件功能以及连接组装试验箱 一.实验内容 研究实验箱串口.USB线的调通连接 二.实践步骤 1.打开实验箱,首先了解各元件功能 这个是LTE模块,也叫4G模块,具体的作用是硬件将 ...
- Kubernetes学习之路(二十三)之资源指标和集群监控
目录 1.资源指标和资源监控 2.Weave Scope监控集群 (1)Weave Scope部署 (2)使用 Scope (3)拓扑结构 (4)实时资源监控 (5)在线操作 (6)强大的搜索功能 2 ...