-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdslabtest.cpp
63 lines (56 loc) · 1.19 KB
/
dslabtest.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
#include<iostream>
#include<cmath>
using namespace std;
class BinaryTreeNode {
public :
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
BinaryTreeNode(int data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
BinaryTreeNode* takeInput(){
int rootData;
cin >> rootData;
if(rootData == -1){
return NULL;
}
BinaryTreeNode* root = new BinaryTreeNode(rootData);
root->left = takeInput();
root->right = takeInput();
return root;
}
int height(BinaryTreeNode *root)
{
if(root== NULL)
{
return 0;
}
int h1=height(root->left);
int h2=height(root->right);
if(h1>h2)
{
return h1+1;
}
else
{
return h2+1;
}
}
int diameter(BinaryTreeNode* root) {
if(root==NULL)
{
return 0;
}
int option1=diameter(root->left);
int option2=diameter(root->right);
int option3=height(root->left)+height(root->right)+1;
return max(option3,max(option2,option1));
}
int main(){
BinaryTreeNode* root = takeInput();
cout << "diameter " << diameter(root) << endl;
}