-
Notifications
You must be signed in to change notification settings - Fork 0
/
cousins_in_binary_tree2.c++
77 lines (73 loc) · 2.28 KB
/
cousins_in_binary_tree2.c++
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
68
69
70
71
72
73
74
75
76
77
/**
* 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:
TreeNode* replaceValueInTree(TreeNode* root) {
// same depth with the different parent;
// simply meanswe have to associate each one with depth and parent
// plan is ki we will first kind of get the vector
// of ans
unordered_map<TreeNode*,TreeNode*>mp;
mp[root]=NULL;
// we will make unordered_map which has a sum of same parent nodes
unordered_map<TreeNode*,int>mp2;
mp[NULL]=0;
queue<TreeNode*>q;
q.push(root);
while(!q.empty()){
int n=q.size();
for(int i=0;i<n;i++){
TreeNode*a=q.front();
q.pop();
int sum=0;
if(a->left!=NULL){
q.push(a->left);
mp[a->left]=a;
sum=sum+a->left->val;
}
if(a->right!=NULL){
q.push(a->right);
mp[a->right]=a;
sum=sum+a->right->val;
}
mp2[a]=sum;
}
}//mapping is done my friends
q.push(root);
while(!q.empty()){
int n=q.size();
vector<TreeNode*>arr;
int sum=0;
for(int i=0;i<n;i++){
TreeNode*a=q.front();
q.pop();
arr.push_back(a);
sum=sum+a->val;
if(a->left!=NULL){
q.push(a->left);
}
if(a->right!=NULL){
q.push(a->right);
}
}
vector<int>arr2(n);
for(int i=0;i<n;i++){
arr2[i]=sum-mp2[mp[arr[i]]];
}
for(int i=0;i<n;i++){
arr[i]->val=arr2[i];
}
}
root->val=0;
return root;
}
};