-
Notifications
You must be signed in to change notification settings - Fork 4
/
height_binary_tree.C
60 lines (55 loc) · 1.1 KB
/
height_binary_tree.C
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
//author @ Ayush Aggarwal
//iterative method to calculate the height of tree
// height of tree = number of nodes on longest path from root to deepest node **<<<**
// height of tree = number of edges on longest path from root to deepest node
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left, *right;
};
struct node* newNode(int data)
{
node *temp = new node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int height_binary_tree(struct node *root)
{
int count = 0;
if(root == NULL)
return 0;
queue <node *> q;
q.push(root);
while(true)
{
int nodeCount = q.size();
if(nodeCount == 0)
return count;
count++;
while(nodeCount > 0)
{
node *temp = q.front();
q.pop();
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
nodeCount--;
}
}
return count;
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
cout<< height_binary_tree(root);
return 0;
}