1- {
1+ // { Driver Code Starts
2+ // Initial Template for C++
3+
24#include < bits/stdc++.h>
35using namespace std ;
4- /* A binary tree node has data, pointer to left child
5- and a pointer to right child */
6+
7+ // Tree Node
68struct Node
79{
810 int data;
9- struct Node * left;
10- struct Node * right;
11- Node (int x){
12- data = x;
13- left = right = NULL ;
14- }
11+ Node* left;
12+ Node* right;
1513};
16- void topView ( struct Node *root);
17- /* Driver program to test size function */
18- int main ( )
14+
15+ // Utility function to create a new Tree Node
16+ Node* newNode ( int val )
1917{
20- int t;
21- struct Node *child;
22- cin >> t;
23- while (t--)
24- {
25- map<int , Node*> m;
26- int n;
27- cin >> n;
28- struct Node *root = NULL ;
29- while (n--)
30- {
31- Node *parent;
32- char lr;
33- int n1, n2;
34- scanf (" %d %d %c" , &n1, &n2, &lr);
35- if (m.find (n1) == m.end ())
36- {
37- parent = new Node (n1);
38- m[n1] = parent;
39- if (root == NULL )
40- root = parent;
41- }
42- else
43- parent = m[n1];
44- child = new Node (n2);
45- if (lr == ' L' )
46- parent->left = child;
47- else
48- parent->right = child;
49- m[n2] = child;
50- }
51- topView (root);
52- cout << endl;
53- }
54- return 0 ;
18+ Node* temp = new Node;
19+ temp->data = val;
20+ temp->left = NULL ;
21+ temp->right = NULL ;
22+
23+ return temp;
5524}
5625
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;
5788}
58- /* This is a function problem.You only need to complete the function given below*/
59- // Structure of binary tree
60- /* struct Node
61- struct Node
89+
90+
91+ // } Driver Code Ends
92+
93+
94+ class Solution
6295{
63- int data;
64- struct Node* left;
65- struct Node* right;
96+ public:
97+
98+ void horizontalDistanceHelper (Node *root, map<int , int > &mp, int hd) {
99+ if (!root) return ;
100+
101+ if (mp.find (hd) == mp.end ()) {
102+ mp[hd] = root->data ;
103+ }
104+
105+ if (root->left )
106+ horizontalDistanceHelper (root->left , mp, hd - 1 );
107+
108+ if (root->right )
109+ horizontalDistanceHelper (root->right , mp, hd + 1 );
110+ }
111+
112+ // Function to return a list of nodes visible from the top view
113+ // from left to right in Binary Tree.
66114
67- Node(int x){
68- data = x;
69- left = right = NULL;
115+ // recursive -- doesnt work correctly for some reason
116+ vector<int > topView (Node *root)
117+ {
118+ // Your code here
119+ if (!root) return {};
120+
121+ map<int , int > mp;
122+ vector<int > result;
123+ horizontalDistanceHelper (root, mp, 0 );
124+ for (auto it : mp) {
125+ result.push_back (it.second );
126+ }
127+ return result;
70128 }
71- };*/
72- // function should print the topView of the binary tree
73- void topView (struct Node *root)
74- {
75- // Your code here
76129
77- map<int , int > m; // node->data, vertical_dist
78- queue<pair<Node*, int >> q; // node, verticaL_dist
79- q.push ({root, 0 });
80- while (!q.empty ()){
81- pair<Node*, int > top = q.front ();
82- Node* c = top.first ;
83- int hd = top.second ;
84- q.pop ();
85- if (m.find (hd) == m.end ()){
86- // v.push_back(c->data);
87- m[hd] = c->data ;
130+
131+ // iterative
132+ vector<int > topView (Node *root)
133+ {
134+ if (!root) return {};
135+
136+ vector<int > result;
137+ map<int , int > mp; // node->data, hd
138+ queue<pair<Node*, int >> q; // node, hd
139+ q.push ({root, 0 });
140+
141+ while (!q.empty ()) {
142+ pair<Node*, int > top = q.front ();
143+ Node* c = top.first ;
144+ int hd = top.second ;
145+ q.pop ();
146+
147+ if (mp.find (hd) == mp.end ()){ // only when we encounter first time
148+ mp[hd] = c->data ;
149+ }
150+
151+ if (c->left )
152+ q.push ({c->left , hd - 1 });
153+
154+ if (c->right )
155+ q.push ({c->right , hd + 1 });
88156 }
89- if (c->left )
90- q.push ({c->left , hd - 1 });
91157
92- if (c->right )
93- q.push ({c->right , hd + 1 });
158+ for (auto i: mp)
159+ result.push_back (i.second );
160+ return result;
161+ }
162+ };
163+
164+
165+
166+ // { Driver Code Starts.
167+
168+ int main () {
169+ int tc;
170+ cin>>tc;
171+ cin.ignore (256 , ' \n ' );
172+ while (tc--) {
173+ string treeString;
174+ getline (cin, treeString);
175+ Solution ob;
176+ Node *root = buildTree (treeString);
177+ vector<int > vec = ob.topView (root);
178+ for (int x : vec)
179+ cout<<x<<" " ;
180+ cout<<endl;
94181 }
95- for (auto i: m)
96- cout<<i.second <<" " ;
97- }
182+ return 0 ;
183+ } // } Driver Code Ends
0 commit comments