forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeheight.cpp
More file actions
49 lines (40 loc) · 692 Bytes
/
Treeheight.cpp
File metadata and controls
49 lines (40 loc) · 692 Bytes
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
#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node* left;
node* right;
};
int maxDepth(node* node)
{
if (node == NULL)
return 0;
else
{
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
if (lDepth > rDepth)
return(lDepth + 1);
else return(rDepth + 1);
}
}
node* newNode(int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
int main()
{
node *root = newNode(1);
root->left = newNode(4);
root->right = newNode(8);
root->left->left = newNode(14);
root->left->right = newNode(15);
cout << "Height of tree : " << maxDepth(root);
return 0;
}