-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtree_diameter.cpp
47 lines (39 loc) · 986 Bytes
/
tree_diameter.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
#include <iostream>
#include <algorithm>
using namespace std;
struct Node
{
int key;
Node *left, *right;
};
struct Node* newNode(int key)
{
struct Node * node = new Node;
node->key = key;
node->left = node->right = nullptr;
return node;
}
int height(struct Node * node)
{
//base case
if (node == nullptr) {return 0;}
return 1 + max(height(node->left), height(node->right));
}
int diameter(struct Node * root)
{
if (root == nullptr) {return 0;}
//case 1: diameter goes through the root
int res = height(root->left) + height(root->right);
//case 2: diameter doesn't go through the root
return max(res, max(diameter(root->left), diameter(root->right)));
}
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 << "Diameter of the binary tree is: " << diameter(root) << endl;
return 0;
}