-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathheight_bt.cpp
More file actions
64 lines (64 loc) · 1.05 KB
/
height_bt.cpp
File metadata and controls
64 lines (64 loc) · 1.05 KB
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
#include <iostream>
#include<queue>
using namespace std;
class node{
public:
int data;
node* left;
node* right;
node(int d)
{
data=d;
left=NULL;
right=NULL;
}
};
node* create(node* root)
{
int d;
cout<<"enter root"<<endl;
cin>>d;
if(d!=-1)
{
root=new node(d);
cout<<"enter left node"<<endl;
root->left=create(root->left);
cout<<"enter right node"<<endl;
root->right=create(root->right);
return root;
}
else
return NULL;
}
void traversal(node* root)
{
queue<node*>q;
q.push(root);
// q.push(NULL);
while(!q.empty())
{
node* temp=q.front();
cout<<temp->data<<" ";
q.pop();
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
}
}
int height(node* root)
{
if(root==NULL)
return 0;
int l=height(root->left);
int r=height(root->right);
return max(l,r)+1;
}
int main()
{
node* root;
root=create(root);
cout<<height(root)<<endl;
traversal(root);
return 0;
}