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