-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththirtyNinth.cpp
57 lines (55 loc) · 1.18 KB
/
thirtyNinth.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
#include <iostream>
#include <queue>
using namespace std;
struct node {
int data;
node* right;
node* left;
};
node* getNewNode(int x) {
node* temp = new node();
temp->data = x;
temp->right = NULL;
temp->left = NULL;
return temp;
}
queue<node*> Q;
int level = 1;
node* root = getNewNode(10);
void create() {
root->right = getNewNode(30);
root->left = getNewNode(20);
root->right->right = getNewNode(70);
root->right->left = getNewNode(60);
root->left->left = getNewNode(40);
root->left->right = getNewNode(50);
}
void heightlevelOrder() {
Q.push(root);
Q.push(NULL);
while (!Q.empty()) {
root = Q.front();
Q.pop();
if (root == NULL) {
if (!Q.empty()) {
Q.push(NULL);
}
level++;
}
else{
if (root->left != NULL) {
Q.push(root->left);
}
if (root->right != NULL) {
Q.push(root->right);
}
}
}
}
int main(int argc, char const *argv[]) {
create();
heightlevelOrder();
std::cout << "The height of the tree is " << '\n';
std::cout << level << '\n';
return 0;
}