-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path59_binaryTree_checkSumTree.cpp
More file actions
65 lines (60 loc) · 1.32 KB
/
59_binaryTree_checkSumTree.cpp
File metadata and controls
65 lines (60 loc) · 1.32 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
#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
node *buildingTree(node *root)
{
cout << "Enter the data: " << endl;
int data;
cin >> data;
root = new node(data);
if (data == -1)
return NULL;
cout << "Enter data to insert at left: " << data << endl;
root->left = buildingTree(root->left);
cout << "Enter data to insert at right: " << data << endl;
root->right = buildingTree(root->right);
return root;
}
bool checkSum(node* root){
if(root == NULL)
return true;
bool left = checkSum(root->left);
bool right = checkSum(root->right);
int lsum = 0;
int rsum = 0;
if (root -> left != NULL)
{
lsum = root->left->data;
}
if (root -> right != NULL)
{
rsum = root->right->data;
}
bool currentNodeData(root->data == lsum + rsum);
return left && right && currentNodeData;
}
int main()
{
node *root = NULL;
root = buildingTree(root);
if (checkSum(root))
{
cout << "The tree follows the children sum property." << endl;
}
else
{
cout << "The tree does not follow the children sum property." << endl;
}
}