-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrint Level Wise
56 lines (48 loc) · 994 Bytes
/
Print Level Wise
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
/**********************************************************
Following is the Binary Tree Node class structure
template <typename T>
class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/
#include <queue>
void printLevelWise(BinaryTreeNode<int> *root) {
if(root==NULL){
return ;
}
queue <BinaryTreeNode<int>*> q;
q.push(root);
while(!q.empty()){
BinaryTreeNode<int> *first = q.front();
q.pop();
if(first->left){
q.push(first->left);
}
if(first->right){
q.push(first->right);
}
cout<<first->data<<":";
if(first->left!=NULL){
cout<<"L:"<<first->left->data;
}
else{
cout<<"L:"<<-1;
}
cout<<",";
if(first->right!=NULL){
cout<<"R:"<<first->right->data;
}
else{
cout<<"R:"<<-1;
}
cout<<endl;
}
}