-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.cpp
181 lines (144 loc) · 5.21 KB
/
solution.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#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;
}
};
class Solution {
public:
Node* insert(Node* root, int data) {
if(root == NULL) {
return new Node(data);
} else {
Node* cur;
if(data <= root->data) {
cur = insert(root->left, data);
root->left = cur;
} else {
cur = insert(root->right, data);
root->right = cur;
}
return root;
}
}
/*
class Node {
public:
int data;
Node *left;
Node *right;
Node(int d) {
data = d;
left = NULL;
right = NULL;
}
};
*/
void topView(Node * root) {
queue<pair<int,Node*>> q; q.push(make_pair(0,root)); //making a queue pair with the distance the node and assigning to root node with nil distance initially
map<int,Node*> ans; //mapping the answer
for(auto i=q.front();!q.empty();q.pop(),i=q.front()){ //till the queue is not empty
if(!i.second) continue;
ans.insert(i);
//pushing node and horizontal distance to queue
q.push(make_pair(i.first+1,i.second->right));
q.push(make_pair(i.first-1,i.second->left));
}
for(auto i:ans) cout<<i.second->data<<" ";
}
};
int main() {
Solution myTree;
Node* root = NULL;
int t;
int data;
std::cin >> t;
while(t-- > 0) {
std::cin >> data;
root = myTree.insert(root, data);
}
myTree.topView(root);
return 0;
}
// We have to print the nodes which we see from the top starting from left to root to Right.
/* Here Level Order traversal will be necessary as we have to Know what Nodes are at what Vertical Distance
--> More Importantly We have to get the Horizontal Distance of Each node from which we can observe whether it will be Visible or Covered by other node
We will need a unordered map to store Horizontal distance.
Array will not work as Nodes address are Random.
We will also need to map the Node which appeared first in Level order as it will be only Node visible from the Top.
*/
unordered_map<Node*,int> hori_dist;
// Function to compute horizontal distance of Each node
void Compute_hzdist(Node *root, int curr_dist)
{
if(root == NULL) // Base condition.
return;
if(root->left != NULL) // If left child exist, update it in map to its distance to curr_dist - 1.
{
hori_dist[root->left] = curr_dist-1;
Compute_hzdist(root->left,curr_dist-1);
}
if(root->right != NULL)
{
hori_dist[root->right] = curr_dist+1; // If right child exist, update it in map to its distance to curr_dist + 1.
Compute_hzdist(root->right,curr_dist+1);
}
}
//
void topView(Node * root)
{
if(root == NULL) // Base condition
{
return;
}
Compute_hzdist(root,0); // Calling function to compute horizontal distance. root's distance = 0;
map<int,int> top; // Map to store Top element which came in level order first corresponding to Horizontal distance..
// distance --> first appeared Node's data
// Applying level Order Traversal
queue<Node*> q;
top[0] = root->data; // for distance = 0 we will see root's data;
q.emplace(root);
Node *p;
while(!q.empty())
{
p = q.front();
q.pop();
if(p->left)
{
int dist = hori_dist[p->left]; // If left child exists we can check if It occured for first time;
// If Yes then add it to map;
if(top.count(dist)==0)
{
top[dist] = p->left->data;
}
q.emplace(p->left);
}
if(p->right)
{
int dist = hori_dist[p->right]; // If right child exists we can check if It occured for first time;
// If Yes then add it to map;
if(top.count(dist)==0)
{
top[dist] = p->right->data;
}
q.emplace(p->right);
}
}
map<int,int>::iterator itr;
for(itr = top.begin();itr!=top.end();itr++)
{
cout<<itr->second<<" "; // printing data in map representing Top View.
}
}
// *** NOTE :
// If we had to to bottom View, Only change is that we will need the last Node occured at that horizontal distance rather tha first.
// time complexity - O(nodes) = O(n);
// Space complexity - O(nodes) = O(n);
};