forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09.02.2024.cpp
47 lines (38 loc) · 951 Bytes
/
09.02.2024.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
/*Complete the function below
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){80
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.
bool f(Node* root){
if(root==NULL) return true;
if(!root->left && !root->right) return true;
bool left=true;
bool right=true;
int sum=0;
if(root->left !=NULL){
sum+=root->left->data;
if(f(root->left)==false) return false;
}
if(root->right !=NULL){
sum+=root->right->data;
if(f(root->right)==false) return false;
}
return root->data==sum;
}
int isSumProperty(Node *root)
{
// Add your code here
return f(root);
}
};