-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path145.binary-tree-postorder-traversal-iteration.c
173 lines (141 loc) · 3.41 KB
/
145.binary-tree-postorder-traversal-iteration.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#define Error(Str) FatalError(Str)
#define FatalError(Str) fprintf(stderr, "%s\n", Str), exit(1)
#define ElementType struct TreeNode *
/**
* Definition for a binary tree node.
*/
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
struct Node
{
ElementType element;
PtrToNode next;
};
int isEmpty(Stack s)
{
return s->next == NULL;
}
void push(ElementType x, Stack s)
{
PtrToNode newCell;
newCell = malloc(sizeof(struct Node));
if (newCell == NULL)
Error("Must use createStack first");
else
{
newCell->element = x;
newCell->next = s->next;
s->next = newCell;
}
}
void pop(Stack s)
{
PtrToNode FirstCell;
if (isEmpty(s))
Error("Empty Stack");
else
{
FirstCell = s->next;
s->next = s->next->next;
free(FirstCell);
}
}
void makeEmpty(Stack s)
{
if (s == NULL)
Error("Must use createStack first");
else
while (!isEmpty(s))
pop(s);
}
Stack createStack()
{
Stack s;
s = malloc(sizeof(struct Node));
if (s == NULL)
FatalError("Out of Memory!!!");
s->next = NULL;
makeEmpty(s);
return s;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int *postorderTraversal(struct TreeNode *root, int *returnSize)
{
*returnSize = 0;
int *result = (int *)malloc(sizeof(int) * 999);
if (root == NULL)
{
return result;
}
Stack output = createStack();
Stack s = createStack();
struct TreeNode *node = root;
push(node, s);
while (!isEmpty(s))
{
node = s->next->element;
pop(s);
push(node, output);
if (node->left != NULL)
push(node->left, s);
if (node->right != NULL)
push(node->right, s);
}
while (!isEmpty(output))
{
node = output->next->element;
pop(output);
result[(*returnSize)++] = node->val;
}
free(output);
free(s);
return result;
}
void printArr(int v[], int len)
{
int i;
for (i = 0; i < len; i++)
printf("%d ", v[i]);
printf("\n");
}
int main()
{
struct TreeNode *rootRightLeftRight = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightLeftRight->val = 4;
rootRightLeftRight->left = rootRightLeftRight->right = NULL;
struct TreeNode *rootRightLeftLeft = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightLeftLeft->val = 5;
rootRightLeftLeft->left = rootRightLeftLeft->right = NULL;
struct TreeNode *rootRightLeft = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightLeft->val = 3;
rootRightLeft->left = rootRightLeftLeft;
rootRightLeft->right = rootRightLeftRight;
struct TreeNode *rootRight = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRight->val = 2;
rootRight->left = rootRightLeft;
rootRight->right = NULL;
struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = 1;
root->left = NULL;
root->right = rootRight;
int resultSize = 0;
int *result = postorderTraversal(root, &resultSize);
printArr(result, resultSize);
free(result);
free(rootRightLeftRight);
free(rootRightLeftLeft);
free(rootRightLeft);
free(rootRight);
free(root);
}