-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcheck-for-children-sum-property-in-a-binary-tree.cpp
More file actions
executable file
·46 lines (43 loc) · 1.28 KB
/
check-for-children-sum-property-in-a-binary-tree.cpp
File metadata and controls
executable file
·46 lines (43 loc) · 1.28 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
//http://www.geeksforgeeks.org/check-for-children-sum-property-in-a-binary-tree/
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *left;
node *right;
};
node * getnewnode(int data){
node *newnode = new node();
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
}
int check(node *root){
if(root == NULL || (root->left == NULL && root->right == NULL)) return 1;
int x=0,y=0;
if(root->left != NULL) x = root->left->data;
if(root->right != NULL) y = root->right->data;
if(root->data == x+ y && check(root->left) && check(root->right)) return 1;
else return 0;
// if(root->left != NULL || root->right != NULL){
// if(root->left != NULL) x = root->left->data;
// if(root->right != NULL) y = root->right->data;
// if(root->data == x+y){
// check(root->left);
// check(root->right);
// }else if(root->left == NULL && root->right == NULL) return 1;
// else{
// return 0;
// }
// }
}
int main(){
node * root = getnewnode(10);
root->left = getnewnode(8);
root->right = getnewnode(2);
root->left->left = getnewnode(5);
root->left->right = getnewnode(2);
int x = check(root);
cout<<x<<endl;
return 0;
}