-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion14.c
66 lines (53 loc) · 1.39 KB
/
question14.c
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
66
/*
Check whether the given binary tree is sum tree or not
Sum tree is the one where sum of values in the LST and sum of values in the RST is equal to the root.
This is valid for all the nodes except the tree
METHOD:
We do post order traversal of the tree where first we compute LST, then RST then we compare if root value
is equal to LST value and RST value sum else we return -1. If equal we return sum of RST, LST and root->data
Time complexity: O(n)
Space complexity: O(n)
*/
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right= NULL;
return temp;
}
int checkSum(struct node *root){
if(!root){
return 0;
}
if(!root->left && !root->right){
return root->data;
}
int left = checkSum(root->left);
int right = checkSum(root->right);
if(root->data == left + right){
return 2*root->data;
}
return -1;
}
int main(){
struct node *root = newNode(50);
root->left = newNode(15);
root->left->left = newNode(10);
root->left->right = newNode(5);
root->right = newNode(10);
root->right->left = newNode(7);
root->right->right = newNode(3);
int isSumTree = checkSum(root);
if(isSumTree > 0){
printf("given tree is a sum tree\n");
}else{
printf("given tree is not a sum tree\n");
}
return 0;
}