-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum tree.c
86 lines (79 loc) · 1.51 KB
/
sum tree.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *l;
struct node *r;
};
typedef struct node node;
node *root=NULL;
node *temp=NULL;
node * create(int d)
{
node *ne=malloc(sizeof(node));
ne->data=d;
ne->l=NULL;
ne->r=NULL;
return ne;
}
void inorder(node *ro)
{
if(ro!=NULL)
{
inorder(ro->l);
printf("%d ",ro->data);
inorder(ro->r);
}
}
int sum_tree(node * root)
{
int s1=0,s2=0;
if(root==NULL)
return 0;
else if (root->l==NULL && root->r==NULL )
return root->data;
else
{
s1=sum_tree(root->l) ;
s2=sum_tree(root->r);
if((s1+s2)==root->data)
{
return s1+s2+root->data;
}
else
return 0;
}
}
int doo(node *ptr)
{
int value=0;
if(ptr!=NULL)
{
if(ptr->l!=NULL)
value=1+doo(ptr->l);
if(ptr->r!=NULL)
value=value>(1+doo(ptr->r))?value:1+doo(ptr->r);
}
return (value);
}
void main()
{
int a,h,n,j;
printf("Enter the values to enter in tree\n");
root=create(26);
root->l=create(10);
root->r=create(3);
root->l->l=create(4);
root->l->r=create(6);
root->r->r=create(3);
inorder(root);
printf("\n");
if( (2*root->data)==sum_tree(root))
printf("Sum tree");
else
printf("Not a sum tree");
printf("%d",doo(root));
getch();
}