-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgiven_tree_is_a_SumTree.cpp
More file actions
49 lines (39 loc) · 1.09 KB
/
Copy pathgiven_tree_is_a_SumTree.cpp
File metadata and controls
49 lines (39 loc) · 1.09 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
#include <stdio.h>
#include <malloc.h>
#include <limits.h>
/* nodes for queue and tree node*/
struct node {
int key;
struct node * left;
struct node * right;
};
/* function for creating new node of tree*/
struct node *newNode(int data)
{
struct node *node = (struct node *)malloc(sizeof(struct node));
node->key = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/*fuction for converting a binary tree into sum tree*/
int isSumTree(struct node * root){
if(root == NULL){
return 0 ;
}
if(root->left == NULL && root->right == NULL)return root->key;
if(root->key == + isSumTree(root->left) + isSumTree(root->right)) return 2*root->key;
return INT_MIN;
}
int main(){
struct node *root = newNode(44);
root->left = newNode(9);
root->right = newNode(13);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
if (isSumTree(root) != INT_MIN ){printf("this is a sum tree");}
else{printf("this is not a sum tree");}
return 0;
}