-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdiameter_0f_binary_tree.cpp
34 lines (27 loc) · 1.08 KB
/
diameter_0f_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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int height(TreeNode* root, int &diameter) {
if(!root) return 0;
int leftHeight = height(root->left, diameter);
int rightHeight = height(root->right, diameter);
diameter = max(diameter, leftHeight + rightHeight + 1); //this is the smart trick to avoid computing recursive diameter again here...at any point in the recursion..diameter will be left + right + 1
return 1 + max(leftHeight, rightHeight); //standard height recursive call..
}
public:
int diameterOfBinaryTree(TreeNode* root) {
int diameter = INT_MIN;
height(root, diameter);
return diameter == INT_MIN ? 0 : diameter-1;
}
};