-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion13.c
75 lines (65 loc) · 1.72 KB
/
question13.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
/*
Vertical sum of the given binary tree
METHOD 1 of previous question (question12). Calculating sum by passing address of variable in print function.
Time and space complexity is the same.
METHOD2 (hashing) and METHOD3 (DLL) can also be applied, where the cell or node will contain the sum
apart from the pointers to the linked list containing different nodes from the tree. Time and space
complexity remains the same.
*/
#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;
}
void findRange(struct node *root, int *min, int *max, int dist){
if(!root){
return;
}
if(dist < *min){
*min = dist;
}
if(dist > *max){
*max = dist;
}
findRange(root->left, min,max,dist-1);
findRange(root->right, min,max,dist+1);
}
void printNodes(struct node *root, int dist,int k, int *sum){
if(!root){
return;
}
if(k == dist){
*sum += root->data;
}
printNodes(root->left,dist-1,k,sum);
printNodes(root->right,dist+1,k,sum);
}
int main(){
int min = 0,max = 0, distance = 0,sum=0;
struct node *root = newNode(10);
root->left = newNode(12);
root->left->left = newNode(14);
root->left->right = newNode(16);
root->right = newNode(20);
root->right->left = newNode(22);
root->right->right = newNode(26);
findRange(root,&min, &max, distance);
printf("min and max values are %d and %d\n", min,max);
printf("printing numbers range-wise\n");
for(int i=min; i<=max;i++){
printf("printing for %d\n", i);
printNodes(root, distance,i, &sum);
printf("sum is %d\n", sum);
sum = 0;
printf("-----------------\n");
}
return 0;
}