1
+ // { Driver Code Starts
2
+ // Initial Template for C++
3
+
4
+ #include < bits/stdc++.h>
5
+ using namespace std ;
6
+
7
+ // Tree Node
8
+ struct Node
9
+ {
10
+ int data;
11
+ Node* left;
12
+ Node* right;
13
+ };
14
+
15
+ // Utility function to create a new Tree Node
16
+ Node* newNode (int val)
17
+ {
18
+ Node* temp = new Node;
19
+ temp->data = val;
20
+ temp->left = NULL ;
21
+ temp->right = NULL ;
22
+
23
+ return temp;
24
+ }
25
+
26
+ // Function to Build Tree
27
+ Node* buildTree (string str)
28
+ {
29
+ // Corner Case
30
+ if (str.length () == 0 || str[0 ] == ' N' )
31
+ return NULL ;
32
+
33
+ // Creating vector of strings from input
34
+ // string after spliting by space
35
+ vector<string> ip;
36
+
37
+ istringstream iss (str);
38
+ for (string str; iss >> str; )
39
+ ip.push_back (str);
40
+
41
+ // Create the root of the tree
42
+ Node* root = newNode (stoi (ip[0 ]));
43
+
44
+ // Push the root to the queue
45
+ queue<Node*> queue;
46
+ queue.push (root);
47
+
48
+ // Starting from the second element
49
+ int i = 1 ;
50
+ while (!queue.empty () && i < ip.size ()) {
51
+
52
+ // Get and remove the front of the queue
53
+ Node* currNode = queue.front ();
54
+ queue.pop ();
55
+
56
+ // Get the current node's value from the string
57
+ string currVal = ip[i];
58
+
59
+ // If the left child is not null
60
+ if (currVal != " N" ) {
61
+
62
+ // Create the left child for the current node
63
+ currNode->left = newNode (stoi (currVal));
64
+
65
+ // Push it to the queue
66
+ queue.push (currNode->left );
67
+ }
68
+
69
+ // For the right child
70
+ i++;
71
+ if (i >= ip.size ())
72
+ break ;
73
+ currVal = ip[i];
74
+
75
+ // If the right child is not null
76
+ if (currVal != " N" ) {
77
+
78
+ // Create the right child for the current node
79
+ currNode->right = newNode (stoi (currVal));
80
+
81
+ // Push it to the queue
82
+ queue.push (currNode->right );
83
+ }
84
+ i++;
85
+ }
86
+
87
+ return root;
88
+ }
89
+
90
+
91
+ // } Driver Code Ends
92
+ /*
93
+ struct Node
94
+ {
95
+ int data;
96
+ Node* left;
97
+ Node* right;
98
+ };
99
+ */
100
+ class Solution
101
+ {
102
+ public:
103
+ // Function to return a list of nodes visible from the top view
104
+ // from left to right in Binary Tree.
105
+ vector<int > topView (Node *root)
106
+ {
107
+ vector<int >ans;
108
+ if (root==NULL )
109
+ return ans;
110
+ // HD Node->data
111
+ map<int , int >topNode;
112
+ // Node HD
113
+ queue<pair<Node*, int > > q;
114
+ q.push (make_pair (root, 0 ));
115
+ while (!q.empty ())
116
+ {
117
+ pair<Node*, int >temp=q.front ();
118
+ q.pop ();
119
+
120
+ Node* frontNode=temp.first ;
121
+ int hd=temp.second ;
122
+
123
+ // If one value is present for a HD then do nothing
124
+ if (!topNode[hd])
125
+ topNode[hd]=frontNode->data ;
126
+
127
+
128
+ if (frontNode->left )
129
+ q.push (make_pair (frontNode->left , hd-1 ));
130
+ if (frontNode->right )
131
+ q.push (make_pair (frontNode->right , hd+1 ));
132
+ }
133
+
134
+ for (auto i:topNode)
135
+ ans.push_back (i.second );
136
+
137
+ return ans;
138
+ }
139
+
140
+ };
141
+
142
+
143
+
144
+ // { Driver Code Starts.
145
+
146
+ int main () {
147
+ int tc;
148
+ cin>>tc;
149
+ cin.ignore (256 , ' \n ' );
150
+ while (tc--) {
151
+ string treeString;
152
+ getline (cin, treeString);
153
+ Solution ob;
154
+ Node *root = buildTree (treeString);
155
+ vector<int > vec = ob.topView (root);
156
+ for (int x : vec)
157
+ cout<<x<<" " ;
158
+ cout<<endl;
159
+ }
160
+ return 0 ;
161
+ }
162
+ // } Driver Code Ends
0 commit comments