-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcovered_uncovered_nodes.cpp
More file actions
48 lines (38 loc) · 969 Bytes
/
covered_uncovered_nodes.cpp
File metadata and controls
48 lines (38 loc) · 969 Bytes
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
//author : aurav
#include<bits/stdc++.h>
using namespace std;
#define tn TreeNode*
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
long coveredNodes(TreeNode* A) {
queue<tn> q;
q.push(A);
vector<vector<int>> lvls;
while(!q.empty()){
int sz = q.size();
vector<int> lvl;
while(sz--){
tn cur = q.front(); q.pop();
lvl.push_back(cur->val);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
lvls.push_back(lvl);
lvl.clear();
continue;
}
long cover = 0;
long uncover = 0;
for(auto lvl : lvls){
for(int i = 0; i < lvl.size(); ++i){
if(i == 0 || i == lvl.size() - 1) uncover += lvl[i];
else cover += lvl[i];
}
}
long dif = abs(cover - uncover);
return dif;
}