-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_101.cpp
More file actions
55 lines (43 loc) · 1.14 KB
/
Day_101.cpp
File metadata and controls
55 lines (43 loc) · 1.14 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
/*
DAY 101: Diameter of Binary Tree.
https://www.geeksforgeeks.org/diameter-of-a-binary-tree/
QUESTION : 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
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 <= Number of nodes <= 10000
1 <= Data of a node <= 1000
*/
int diameterOpt(Node* root, int* height) {
int left_height = 0, right_height = 0;
int left_dia = 0, right_dia = 0;
if (root == NULL) {
*height = 0;
return 0;
}
left_dia = diameterOpt(root->left, &left_height);
right_dia = diameterOpt(root->right, &right_height);
*height = max(left_height, right_height) + 1;
return max(max(left_dia, right_dia), left_height+right_height+1);
}
int diameter(Node* root) {
int height = 0;
int d = diameterOpt(root, &height);
return d;
}