-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopView.cpp
More file actions
103 lines (74 loc) · 1.92 KB
/
Copy pathtopView.cpp
File metadata and controls
103 lines (74 loc) · 1.92 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#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;
}
};
node* buildTree()
{
int d;
cin>>d;
if(d==-1) return NULL;
else
{
node* root = new node(d);
root->left = buildTree();
root->right = buildTree();
return root;
}
}
// the idea is to have an queue of type<node, hd> and store elements in bfs order.
// Have a map with the hd as the key and their corresponding nodes.
// Finally, the first value of each key will be give the top for the particular hd.
// for bottom view, we just need to replace the value of hd with new value, if encoutered
vector<int> topView(node* root)
{
vector<int> v;
if(root==NULL) return v;
else
{
queue<pair<node*, int>> q;
map<int, int> m;
q.push(make_pair(root, 0));
while(!q.empty())
{
pair<node*, int> front = q.front();
q.pop();
// for bottom view :
// m[front.second] = front.first->data; instead of the below if condition
if(m.find(front.second)==m.end())
{
m[front.second] = front.first->data;
}
if(front.first->left) q.push(make_pair(front.first->left, front.second-1));
if(front.first->right) q.push(make_pair(front.first->right, front.second+1));
}
map<int, int> :: iterator it;
int temp;
for(it=m.begin();it!=m.end();it++)
{
v.push_back(it->second);
}
return v;
}
}
int main()
{
node* root = buildTree();
vector<int> v;
v = topView(root);
for(int i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}
return 0;
}