Skip to content

Commit 52b6408

Browse files
committed
Level order traversal Line by Line in progress
1 parent 4e3b77f commit 52b6408

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
/* A binary tree node has data, pointer to left child
5+
and a pointer to right child */
6+
struct Node
7+
{
8+
int data;
9+
struct Node* left;
10+
struct Node* right;
11+
12+
Node(int x){
13+
data = x;
14+
left = right = NULL;
15+
}
16+
};
17+
void levelOrder(struct Node* node);
18+
/* Helper function to test mirror(). Given a binary
19+
search tree, print out its data elements in
20+
increasing sorted order.*/
21+
void inOrder(struct Node* node)
22+
{
23+
if (node == NULL)
24+
return;
25+
inOrder(node->left);
26+
printf("%d ", node->data);
27+
inOrder(node->right);
28+
}
29+
/* Driver program to test size function*/
30+
int main()
31+
{
32+
int t;
33+
struct Node *child;
34+
scanf("%d
35+
", &t);
36+
while (t--)
37+
{
38+
map<int, Node*> m;
39+
int n;
40+
scanf("%d",&n);
41+
struct Node *root = NULL;
42+
while (n--)
43+
{
44+
Node *parent;
45+
char lr;
46+
int n1, n2;
47+
scanf("%d %d %c", &n1, &n2, &lr);
48+
if (m.find(n1) == m.end())
49+
{
50+
parent = new Node(n1);
51+
m[n1] = parent;
52+
if (root == NULL)
53+
root = parent;
54+
}
55+
else
56+
parent = m[n1];
57+
child = new Node(n2);
58+
if (lr == 'L')
59+
parent->left = child;
60+
else
61+
parent->right = child;
62+
m[n2] = child;
63+
}
64+
levelOrder(root);
65+
cout << endl;
66+
}
67+
return 0;
68+
}
69+
70+
71+
/*This is a function problem.You only need to complete the function given below*/
72+
/* A binary tree Node
73+
struct Node
74+
{
75+
int data;
76+
struct Node* left;
77+
struct Node* right;
78+
79+
Node(int x){
80+
data = x;
81+
left = right = NULL;
82+
}
83+
};
84+
*/
85+
//You are required to complete this method
86+
void levelOrder(Node* node)
87+
{
88+
if(node == NULL)
89+
return;
90+
91+
queue<node*> q;
92+
q.push(node);
93+
while(!q.empty())
94+
{
95+
int size = q.size();
96+
Node* temp = q.front();
97+
q.pop();
98+
}
99+
100+
//Your code here
101+
}

0 commit comments

Comments
 (0)