-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbottom_view.cpp
More file actions
60 lines (58 loc) · 1.18 KB
/
Copy pathbottom_view.cpp
File metadata and controls
60 lines (58 loc) · 1.18 KB
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
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
data=d;
left=NULL;
right=NULL;
}
};
multimap<int, int> m;
void getLevelOrder(node *root){
int n=0;
queue<pair<node *,int>> q;
q.push({root,0});
while(!q.empty()){
pair<node *,int> curr=q.front();
q.pop();
m.insert({curr.second,curr.first->data});
if(curr.first->left){
q.push({curr.first->left,curr.second-1});
}
if(curr.first->right){
q.push({curr.first->right,curr.second+1});
}
}
}
void bottomView(struct node *root)
{
if(root)
getLevelOrder(root);
auto it=m.begin();
it++;
for(auto itr=m.begin();itr!=m.end();itr++)
{
if(it->first != itr->first)
cout<<(itr->second)<<" ";
it++;
}
m.clear();
}
int main()
{
node *root=new node(30);
root->left=new node(20);
root->right=new node(50);
root->left->left=new node(10);
root->right->left=new node(40);
root->right->right=new node(60);
root->right->right->right=new node(70);
bottomView(root);
return 0;
}