-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathKhushi3.cpp
94 lines (94 loc) · 1.5 KB
/
Khushi3.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
using namespace std;
class node
{
public:
int data;
node* left;
node* right;
node(int d)
{
data = d;
left = NULL; //when both the child are their intialize it with the null
right = NULL;
}
};
node * buildtree(node *root)
{
int d;
cin>>d;
if(d == -1)
{
return NULL;
}
node *root = new node(d);
root -> key = key;
root -> left = NULL;
root -> right = NULL;
return root;
}
int size(node * root)
{
if(root == NULL)
{
return 0;
}
else
{
return (size(root->left) + 1 + size(root->right));
}
}
int height(node *root)
{
if(root == NULL)
{
return 0;
}
int ls = height(root->left);
int rs = height(root->right);
return max(ls,rs) + 1;
}
void maxsum(node *root)
{
queue <node *> q;
q.push(root);
while(!q.empty())
{
node *f = q.front();
int max = 0;
if(max < f->data)
{
max = f->data;
}
q.pop();
if(f->left)
{
q.push(f->left);
}
if(f->right)
{
q.push(f->right);
}
}
cout<<max<<endl;
return;
}
int sum(node *root)
{
if(root == NULL)
{
return 0;
}
else
{
return(root->key + sum(root->left) + sum(root->right));
}
}
int main()
{
node *root = buildtree();
cout<<size(root);
cout<<sum(root);
maxsum(root);
cout<<height(root);
}