-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChildren_sum.cpp
44 lines (39 loc) · 950 Bytes
/
Children_sum.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
/*Complete the function below
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
*/
class Solution
{
public:
// Function to check whether all nodes of a tree have the value
// equal to the sum of their child nodes.
int isSumProperty(Node *root)
{
// Add your code here
if (root == NULL)
return 1;
if (root->left == NULL && root->right == NULL)
return 1;
int sum = 0;
if (root->left)
sum += root->left->data;
if (root->right)
sum += root->right->data;
if (root->data == sum && isSumProperty(root->left) && isSumProperty(root->right))
{
return 1;
}
else
{
return 0;
}
}
};