-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathr1.cpp
More file actions
70 lines (62 loc) · 1.17 KB
/
r1.cpp
File metadata and controls
70 lines (62 loc) · 1.17 KB
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
// C++ implementation of
// the above approach
#include <bits/stdc++.h>
using namespace std;
int oddsum = 0;
int evensum = 0;
int ans = 0;
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newnode(int data)
{
node* temp = new node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Function calculate the sum of
// odd and even value node
void OddEvenDifference(struct node*
root)
{
// If root is NULL
if (root == NULL) {
return;
}
else {
// Check if current root
// is odd or even
if (root->data % 2 == 0) {
evensum += root->data;
}
else {
oddsum += root->data;
}
// Call on the left subtree
OddEvenDifference(root->left);
// Call on the right subtree
OddEvenDifference(root->right);
}
}
// Driver Code
int main()
{
node* root = newnode(5);
root->left = newnode(2);
root->right = newnode(6);
root->left->left = newnode(1);
root->left->right = newnode(4);
root->left->right->left
= newnode(3);
root->right->right = newnode(8);
root->right->right->right
= newnode(9);
root->right->right->left
= newnode(7);
OddEvenDifference(root);
cout << abs(oddsum - evensum)
<< endl;
}