-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbinary_tree_least_common_ancestor.c
More file actions
137 lines (118 loc) · 3.07 KB
/
binary_tree_least_common_ancestor.c
File metadata and controls
137 lines (118 loc) · 3.07 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* binary_tree_least_common_ancestor.c - find least common ancestor of two nodes in a binary tree
*
* Author: Wang Guibao <wang_guibao#163.com>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_ELE 32
typedef struct _NODE NODE;
struct _NODE {
NODE *left;
NODE *right;
int n;
};
/**
* construct_binary_tree
* Constructs binary tree from preorder and inorder traversal sequence
* @param preorder preorder traversal sequence
* @param inorder inorder traversal sequence
* @return root of the binary tree
*/
NODE *construct_binary_tree(int *preorder, int *inorder, int n)
{
int a;
int i;
if (n <= 0) {
return NULL;
}
a = preorder[0];
NODE *node = malloc(sizeof(NODE));
node->n = a;
node->left = NULL;
node->right = NULL;
if (n == 1) {
return node;
}
else {
for (i = 0; i < n; i++) {
if (inorder[i] == a) {
break;
}
}
node->left = construct_binary_tree(&preorder[1], inorder, i);
node->right = construct_binary_tree(&preorder[i + 1], &inorder[i + 1], n - (i + 1));
return node;
}
}
/**
* least_common_ancestor
* Find least common ancestor of two nodes in a binary tree.
* For the seek of simplicity, assume all elements in the tree are distinct
*
* @param tree the binary tree
* @param a the first node value
* @param b the second node value
* @return the node of the least common ancestor, otherwise return INFINITY
*/
NODE *least_common_ancestor(NODE *tree, int a, int b)
{
NODE *left;
NODE *right;
if (tree == NULL) {
return NULL;
}
left = least_common_ancestor(tree->left, a, b);
right = least_common_ancestor(tree->right, a, b);
if (left != NULL && right != NULL) {
return tree;
}
else if ((left != NULL || right != NULL) && (tree->n == a || tree->n == b)) {
return tree;
}
else if (tree->n == a || tree->n == b) {
return tree;
}
else if (left != NULL) {
return left;
}
else if (right != NULL) {
return right;
}
else {
return NULL;
}
}
int main()
{
int preorder[MAX_ELE + 1];
int inorder[MAX_ELE + 1];
int n;
int a; /* first node value to find */
int b; /* second node value to find */
int i;
NODE *tree;
NODE *lca;
printf("Number of elements: ");
scanf("%d", &n);
printf("Preorder sequence (max %d, delimit with space): ", MAX_ELE);
for (i = 0; i < n; i++) {
scanf("%d", &preorder[i]);
}
printf("Inorder sequence (max %d, delimit with space): ", MAX_ELE);
for (i = 0; i < n; i++) {
scanf("%d", &inorder[i]);
}
tree = construct_binary_tree(preorder, inorder, n);
printf("Two value in tree (delimit with space): ");
scanf("%d %d", &a, &b);
lca = least_common_ancestor(tree, a, b);
if (lca != NULL) {
printf("%d", lca->n);
}
else {
printf("Find fail!\n");
}
return 0;
}