-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP6_Tree_b.c
58 lines (46 loc) · 1.49 KB
/
P6_Tree_b.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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
int isCommonMember(struct Node* root1, struct Node* root2, int target) {
if (root1 == NULL || root2 == NULL) {
return 0;
}
if (root1->data == target && root2->data == target) {
return 1;
}
int leftCheck = isCommonMember(root1->left, root2->left, target);
int rightCheck = isCommonMember(root1->right, root2->right, target);
return leftCheck || rightCheck;
}
int main() {
// Assume two organizational trees with the same structure
struct Node* root1 = createNode(1);
root1->left = createNode(2);
root1->right = createNode(3);
root1->left->left = createNode(4);
root1->left->right = createNode(5);
struct Node* root2 = createNode(1);
root2->left = createNode(2);
root2->right = createNode(3);
root2->left->left = createNode(6);
root2->left->right = createNode(5);
int target;
printf("Enter the target member to check for commonality: ");
scanf("%d", &target);
if (isCommonMember(root1, root2, target)) {
printf("%d is a common member in both organizations.\n", target);
} else {
printf("%d is not a common member in both organizations.\n", target);
}
return 0;
}