Skip to content

Commit b07d3d6

Browse files
committed
Adds level order traverse 2 solution
1 parent 88bbde7 commit b07d3d6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

c++/level-order-tree2.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://practice.geeksforgeeks.org/problems/level-order-traversal-line-by-line/1/?track=PC-W6-T&batchId=154
2+
void levelOrder(Node* node)
3+
{
4+
if(node == NULL)
5+
return;
6+
7+
queue<Node *>q;
8+
q.push(node);
9+
q.push(NULL);
10+
11+
Node *temp;
12+
while(q.size() >1 )
13+
{
14+
temp = q.front();
15+
q.pop();
16+
if(temp == NULL)
17+
{
18+
cout << "$ ";
19+
q.push(NULL);
20+
continue;
21+
}
22+
23+
cout << temp->data<<" ";
24+
if(temp->left != NULL) q.push(temp->left);
25+
if(temp->right != NULL) q.push(temp->right);
26+
}
27+
cout << "$";
28+
}

0 commit comments

Comments
 (0)