-
Notifications
You must be signed in to change notification settings - Fork 0
/
543-diameter-of-binary-tree.cpp
56 lines (50 loc) · 1.71 KB
/
543-diameter-of-binary-tree.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
// Title: Diameter of Binary Tree
// Description:
// Given a binary tree, you need to compute the length of the diameter of the tree.
// The diameter of a binary tree is the length of the longest path between any two nodes in a tree.
// This path may or may not pass through the root.
// Note: The length of path between two nodes is represented by the number of edges between them.
// Link: https://leetcode.com/problems/diameter-of-binary-tree/
// Time complexity: O(n)
// Space complexity: O(n)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
struct Result {
int maxHeight;
int maxDiameter;
Result(int maxHeight, int maxDiameter) :
maxHeight(maxHeight),
maxDiameter(maxDiameter)
{}
};
public:
int diameterOfBinaryTree(TreeNode *root) {
return maxHeightAndDiameter(root).maxDiameter;
}
Result maxHeightAndDiameter(TreeNode *node) {
if (node == NULL)
return Result(-1, 0);
Result left = maxHeightAndDiameter(node->left);
Result right = maxHeightAndDiameter(node->right);
return Result(
/* max height: */
std::max(left.maxHeight, right.maxHeight) + 1,
/* max diameter: */
std::max(
// if max diameter does pass this node
left.maxHeight + 2 + right.maxHeight,
// if max diameter doesn't pass this node
std::max(left.maxDiameter, right.maxDiameter)
)
);
}
};