-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathlevel_order_traversal.cpp
75 lines (73 loc) · 1.66 KB
/
level_order_traversal.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
// Level order Traversal of a Binary Tree TC O(n^2)
// Program Author : Abhisek Kumar Gupta
/*
40
/ \
10 30
/ \ / \
5 -1 -1 28
/ \ / \
1 -1 15 20
/ \ /\ /\
1 -1 -1 -1 -1 -1
/\
-1 -1
Input : 40 10 5 1 1 -1 -1 -1 -1 -1 30 -1 28 15 -1 -1 20 -1 -1
Output : 40
10 30
5 28
1 15 20
1
*/
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
Node(int x){
data = x;
}
};
Node* build_binary_tree(){
int data;
cin >> data;
if(data == -1){
return NULL;
}
Node* root = new Node(data);
root->left = build_binary_tree();
root->right = build_binary_tree();
return root;
}
int compute_height_of_binary_tree(Node* root){
if(root == NULL)
return 0;
int left_height = compute_height_of_binary_tree(root->left);
int right_height = compute_height_of_binary_tree(root->right);
return max(left_height, right_height) + 1;
}
void print_kth_level(Node* root, int k){
if(root == NULL)
return;
if(k == 1){
cout << root->data << " ";
return;
}
print_kth_level(root->left, k - 1);
print_kth_level(root->right, k- 1);
return;
}
void level_order_traversal(Node* root){
int height = compute_height_of_binary_tree(root);
for(int i = 1; i <= height; i++){
print_kth_level(root, i);
cout << "\n";
}
}
int main(){
Node* root = build_binary_tree();
level_order_traversal(root);
return 0;
}