- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiameter_binarytree.cpp
More file actions
Latest commit
32 lines (31 loc) · 817 Bytes
/
Copy pathdiameter_binarytree.cpp
File metadata and controls
32 lines (31 loc) · 817 Bytes
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
classSolution {
public:
intdiameterOfBinaryTree(TreeNode* root) {
if(root==NULL)
{
return0;
}
int option1 = height(root->left) + height(root->right);
int option2 = diameterOfBinaryTree(root->right);
int option3 = diameterOfBinaryTree(root->left);
returnmax(option1,max(option2,option3));
}
intheight(TreeNode* root){
if(root==NULL)
{
return0;
}
int lef = height(root->left);
int righ = height(root->right);
return1 + max(lef,righ);
}
};