-
Notifications
You must be signed in to change notification settings - Fork 3
/
637.cpp
67 lines (66 loc) · 1.94 KB
/
637.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> mp;
void dfs(TreeNode* node, int level) {
if (node == nullptr) return;
if (mp.size() == level) mp.push_back({node->val});
else mp[level].push_back(node->val);
dfs(node->left, level + 1);
dfs(node->right, level + 1);
}
vector<double> averageOfLevels(TreeNode* root) {
dfs(root, 0);
int n = mp.size();
vector<double> res(n, 0);
for (int i = 0; i < n; ++i) {
double sum = 0;
for (auto& num : mp[i]) sum += num;
sum /= (double)(mp[i].size());
res[i] = sum;
}
return res;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> res;
queue<TreeNode*> q;
if (root) q.push(root);
while (!q.empty()) {
double sum = 0;
int n = q.size();
for (int i = 0; i < n; ++i) {
TreeNode* node = q.front();
q.pop();
sum += node->val;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
res.push_back(sum / n);
}
return res;
}
};