-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.5.1.cpp
63 lines (56 loc) · 1.45 KB
/
17.5.1.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
/*
Level order traversal
Approach
- Use two queues currLevel and nextLevel
- check if root = NULL then return
- else push root to the currLevel
- then define a while loop | cond = currLevel != empty
- inside loop get print currlevel.front and then pop it
- push left and right child of curr to nextLevel
- swap currLevel and nextLevel if currLevel is empty
*/
#include "bits/stdc++.h"
#include <queue>
using namespace std;
struct Node{
int data;
Node *left, *right;
Node(int val){
data = val;
left = NULL;
right = NULL;
}
};
void levelOrderTraverse(Node* root){
if(root == NULL) return;
queue<Node*> currentLevel;
queue<Node*> nextLevel;
currentLevel.push(root);
while(!currentLevel.empty()){
Node* curr = currentLevel.front();
currentLevel.pop();
cout<<curr->data<<" ";
if(curr->left) nextLevel.push(curr->left);
if(curr->right) nextLevel.push(curr->right);
if(currentLevel.empty()){
swap(currentLevel, nextLevel);
}
}
}
int main(){
/*
7
/ \
8 11
/ / \
10 12 34
*/
Node* root = new Node(7);
root->left = new Node(8);
root->left->left = new Node(10);
root->right = new Node(11);
root->right->left = new Node(12);
root->right->right = new Node(34);
levelOrderTraverse(root);
return 0;
}