-
Notifications
You must be signed in to change notification settings - Fork 77
/
LevelOrderTraversal.cpp
108 lines (93 loc) · 2.74 KB
/
LevelOrderTraversal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// For a given a Binary Tree of type integer, print it in a level order fashion where each level will be printed on a new line. Elements on every level will be printed in a linear fashion and a single space will separate them.
// Input Format:
// The first and the only line of input will contain the node data, all separated by a single space. Since -1 is used as an indication whether the left or right node data exist for root, it will not be a part of the node data.
// Output Format:
// The given input tree will be printed in a level order fashion where each level will be printed on a new line.
// Elements on every level will be printed in a linear fashion. A single space will separate them.
// Input :
// 10 20 30 40 50 -1 60 -1 -1 -1 -1 -1 -1
// Output :
// 10
// 20 30
// 40 50 60
#include <iostream>
#include <queue>
using namespace std;
template <typename T>
class BinaryTreeNode {
public:
T data;
BinaryTreeNode<T>* left;
BinaryTreeNode<T>* right;
BinaryTreeNode(T data) {
this->data = data;
left = NULL;
right = NULL;
}
};
BinaryTreeNode<int>* takeInput() {
int rootData;
cin >> rootData;
if (rootData == -1) {
return NULL;
}
BinaryTreeNode<int>* root = new BinaryTreeNode<int>(rootData);
queue<BinaryTreeNode<int>*> q;
q.push(root);
while (!q.empty()) {
BinaryTreeNode<int>* currentNode = q.front();
q.pop();
int leftChild, rightChild;
cin >> leftChild;
if (leftChild != -1) {
BinaryTreeNode<int>* leftNode = new BinaryTreeNode<int>(leftChild);
currentNode->left = leftNode;
q.push(leftNode);
}
cin >> rightChild;
if (rightChild != -1) {
BinaryTreeNode<int>* rightNode =
new BinaryTreeNode<int>(rightChild);
currentNode->right = rightNode;
q.push(rightNode);
}
}
return root;
}
void printLevelWise(BinaryTreeNode<int> *root)
{
if(root == NULL)
{
return;
}
queue<BinaryTreeNode<int>*> pendingNodes;
pendingNodes.push(root);
int next = 0, current = 1;
while(!pendingNodes.empty())
{
BinaryTreeNode<int>* front = pendingNodes.front();
pendingNodes.pop();
cout<<front->data<<" ";
current--;
if(front->left != NULL)
{
pendingNodes.push(front->left);
next++;
}
if(front->right != NULL)
{
pendingNodes.push(front->right);
next++;
}
if(current == 0)
{
current = next;
next = 0;
cout<<endl;
}
}
}
int main() {
BinaryTreeNode<int>* root = takeInput();
printLevelWise(root);
}