forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
106.c
58 lines (44 loc) · 1.48 KB
/
106.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 TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
int searchNode(int inorder[], int inorderSize, int key){
int i;
for (i = 0; i < inorderSize; i++) {
if (key == inorder[i]) {
return i;
}
}
return -1;
}
struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) {
if (inorder == NULL || postorder == NULL
|| inorderSize == 0 || postorderSize == 0) return NULL;
struct TreeNode *root
= (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = postorder[postorderSize - 1];
int index = searchNode(inorder, inorderSize, postorder[postorderSize - 1]);
if (index == -1) return NULL;
root->left = buildTree(inorder, index, postorder, index);
root->right = buildTree(inorder + index + 1, inorderSize - index - 1,
postorder + index, inorderSize - index - 1);
return root;
}
void postOrderPrint(struct TreeNode *root) {
if (root) {
postOrderPrint(root->left);
postOrderPrint(root->right);
printf("%d ", root->val);
}
}
int main() {
int in[] = { 4, 8, 10, 12, 14, 20, 22 };
int post[] = { 4, 10, 14, 12, 8, 22, 20 };
struct TreeNode *r
= buildTree(in, sizeof(in)/sizeof(in[0]), post, sizeof(post)/sizeof(post[0]));
postOrderPrint(r); printf("\n");
return 0;
}