-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiameter_of_Binary_Tree_in_O-n.cpp
77 lines (64 loc) · 1.47 KB
/
Diameter_of_Binary_Tree_in_O-n.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Problem Statement:
-----------------
Given a Binary Tree, find diameter of it.
The diameter of a tree is the number of nodes on the longest path between two end nodes in the tree.
Example 1:
---------
Input:
1
/ \
2 3
Output: 3
Example 2:
---------
Input:
10
/ \
20 30
/ \
40 60
Output: 4
Your Task: You need to complete the function diameter() that takes root as parameter and returns the diameter.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
*/
// Link --> https://practice.geeksforgeeks.org/problems/diameter-of-binary-tree/1#
// Code :
struct node
{
int data;
struct node *left, *right;
};
struct node* newNode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
pair <int,int> utilityDia(struct node* root)
{
pair <int , int> p,lt,rt,answer;
// first --> height
// second --> diameter
if(root == NULL)
{
p.first = -1;
p.second = 0;
return p;
}
lt = utilityDia(root->left);
rt = utilityDia(root->right);
//storing resultant height.
answer.first = max(lt.first , rt.first) + 1;
// passing through root
int dia = lt.first + rt.first + 2;
answer.second = max(dia, max(lt.second , rt.second));
return answer;
}
int diameter(struct node* root)
{
return (utilityDia(root).second + 1);
}