1
+ #include < iostream>
2
+ using namespace std ;
3
+ #define MOD 1000000000 + 7 ;
4
+ // TreeNode
5
+ class Node {
6
+ public:
7
+ int val;
8
+ Node* left;
9
+ Node* right;
10
+ Node (int val){
11
+ this ->val = val;
12
+ this ->left = nullptr ;
13
+ this ->right = nullptr ;
14
+ }
15
+ };
16
+ void display (Node* root){
17
+ if (root==nullptr ) return ;
18
+ cout<<root->val <<endl;
19
+ display (root->left );
20
+ display (root->right );
21
+ }
22
+ int sum_of_nodes (Node* root){
23
+ if (root==nullptr ) return 0 ;
24
+ int sum = root->val + sum_of_nodes (root->left ) + sum_of_nodes (root->right );
25
+ return sum;
26
+ }
27
+ int product (Node *root){
28
+ if (root==nullptr ) return 1 ;
29
+ int prod = root->val * product (root->left ) * product (root->right );
30
+ return prod % MOD;
31
+ }
32
+ int no_of_nodes (Node *root){
33
+ if (root==nullptr ) return 0 ;
34
+ int size = 1 + no_of_nodes (root->left ) + no_of_nodes (root->right );
35
+ return size;
36
+ }
37
+ int maxNode (Node *root){
38
+ if (root==nullptr ) return INT_MIN;
39
+ // to make sure we get the max value
40
+ // when comparing wiht -ve no as well
41
+ // to compare 3 values
42
+ int max_node = max (root->val ,max (maxNode (root->left ),maxNode (root->right )));
43
+ return max_node;
44
+ }
45
+ int level (Node *root){
46
+ if (root==nullptr ) return 0 ;
47
+ int level_size = 1 + max (level (root->left ),level (root->right ));
48
+ return level_size;
49
+ }
50
+ int main (){
51
+ Node* a = new Node (10 );
52
+ Node* b = new Node (20 );
53
+ Node* c = new Node (30 );
54
+ Node* d = new Node (40 );
55
+ Node* e = new Node (50 );
56
+ Node* f = new Node (60 );
57
+ Node* g = new Node (70 );
58
+ a->left = b; a->right = c;
59
+ b->left = d; b->right = e;
60
+ c->left = f; c->right = g;
61
+ // display(a);
62
+ // cout<<sum_of_nodes(a);
63
+ // cout<<no_of_nodes(a);
64
+ // cout<<product(a);
65
+ // cout<<maxNode(a);
66
+ cout<<level (a);
67
+ }
0 commit comments