-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDebthOfBst.cpp
49 lines (49 loc) · 923 Bytes
/
DebthOfBst.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
#include<iostream>
using namespace std;
struct Node{
int data;
Node *left;
Node *right;
};
Node *root=NULL;
Node* Create(int data){
Node *newNode=new Node();
newNode->data=data;
newNode->left=NULL;
newNode->right=NULL;
return newNode;
}
int Debth(Node *temp)
{
if(root=NULL)
{
return 0;
}
else
{
int leftD=0,rightD=0;
if(temp->left!=NULL)
{
leftD=Debth(temp->left);
}
if(temp->right!=NULL)
{
rightD=Debth(temp->right);
}
int max=(leftD>rightD)?leftD:rightD;
return max+1;
}
}
int main()
{
root = Create(1);
root->left = Create(2);
root->right = Create(3);
root->left->left = Create(4);
root->right->left = Create(5);
root->right->right = Create(6);
root->right->right->right= Create(7);
root->right->right->right->right = Create(8);
cout<<"Maximum Debth is : "<<Debth(root);
return 0;
}