-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution2.cpp
41 lines (37 loc) · 998 Bytes
/
Solution2.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
/**
* 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) {}
* };
*/
using ll = long long;
class Solution {
public:
vector<ll> s;
vector<int> cnt;
vector<double> averageOfLevels(TreeNode* root) {
dfs(root, 0);
vector<double> ans(s.size());
for (int i = 0; i < s.size(); ++i) {
ans[i] = (s[i] * 1.0 / cnt[i]);
}
return ans;
}
void dfs(TreeNode* root, int i) {
if (!root) return;
if (s.size() == i) {
s.push_back(root->val);
cnt.push_back(1);
} else {
s[i] += root->val;
cnt[i]++;
}
dfs(root->left, i + 1);
dfs(root->right, i + 1);
}
};