Skip to content

Commit a6a97b5

Browse files
Added solutions
1 parent 21bff32 commit a6a97b5

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

amazon/diagonal_sum.cpp

+14-32
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
1-
multimap <int,int> m;
2-
void inorder(Node * root,int hd) {
1+
void dSumUtil(Node *root, int d, map<int, int> &mp){
32
if(!root) return;
4-
5-
m.insert({hd, root->data});
6-
inorder(root->left, hd+1);
7-
inorder(root->right, hd);
8-
9-
}
10-
11-
void diagonalPrint(Node *root) {
12-
13-
inorder(root,0);
14-
for(auto it=m.begin();it!=m.end();it++)
15-
cout<<it->second<<" ";
16-
m.clear();
17-
3+
mp[d] += root->data;
4+
dSumUtil(root->left, d + 1, mp);
5+
dSumUtil(root->right, d, mp);
186
}
19-
20-
/*void diagonalPrint(Node *root)
7+
void diagonalSum(Node* root)
218
{
22-
// your code here
23-
queue<Node *> q;
24-
q.push(root);
25-
while(!q.empty()){
26-
Node *t=q.front();
27-
while(t){
28-
cout<<t->data<<" ";
29-
if(t->left)
30-
q.push(t->left);
31-
t=t->right;
32-
}
33-
q.pop();
34-
}
35-
}*/
9+
// Add your code here
10+
if(!root) return;
11+
map<int, int> mp;
12+
dSumUtil(root, 0, mp);
13+
14+
for(auto it = mp.begin(); it != mp.end(); it++)
15+
cout<<it->second<<" ";
16+
cout<<"\n";
17+
}

amazon/diagonal_traversal.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
multimap <int,int> m;
2+
void inorder(Node * root,int hd) {
3+
if(!root) return;
4+
5+
m.insert({hd, root->data});
6+
inorder(root->left, hd+1);
7+
inorder(root->right, hd);
8+
9+
}
10+
11+
void diagonalPrint(Node *root) {
12+
13+
inorder(root,0);
14+
for(auto it=m.begin();it!=m.end();it++)
15+
cout<<it->second<<" ";
16+
m.clear();
17+
18+
}
19+
20+
/*void diagonalPrint(Node *root)
21+
{
22+
// your code here
23+
queue<Node *> q;
24+
q.push(root);
25+
while(!q.empty()){
26+
Node *t=q.front();
27+
while(t){
28+
cout<<t->data<<" ";
29+
if(t->left)
30+
q.push(t->left);
31+
t=t->right;
32+
}
33+
q.pop();
34+
}
35+
}*/

0 commit comments

Comments
 (0)