124 lines
1.9 KiB
C
124 lines
1.9 KiB
C
#define S2
|
|
struct TreeNode
|
|
{
|
|
int val;
|
|
struct TreeNode *left;
|
|
struct TreeNode *right;
|
|
};
|
|
|
|
#ifdef S1
|
|
|
|
#include <limits.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
|
|
struct list_t
|
|
{
|
|
int *data;
|
|
size_t idx;
|
|
size_t capacity;
|
|
|
|
void (*append)(struct list_t *, int);
|
|
void (*delete)(struct list_t *);
|
|
};
|
|
|
|
void append(struct list_t *self, int v)
|
|
{
|
|
if (self->idx >= self->capacity)
|
|
{
|
|
self->data = (int *)realloc(self->data, sizeof(int) * (self->capacity + 5));
|
|
self->capacity += 5;
|
|
}
|
|
|
|
self->data[self->idx++] = v;
|
|
}
|
|
|
|
void delete(struct list_t *self)
|
|
{
|
|
free(self->data);
|
|
self->idx = 0;
|
|
self->capacity = 0;
|
|
}
|
|
|
|
void init_list(struct list_t *list)
|
|
{
|
|
if (!list)
|
|
return;
|
|
|
|
list->data = (int *)malloc(sizeof(int) * 5);
|
|
list->capacity = 5;
|
|
list->idx = 0;
|
|
|
|
list->append = &append;
|
|
list->delete = &delete;
|
|
}
|
|
|
|
void inorder(struct TreeNode *n, struct list_t *l)
|
|
{
|
|
if (!n)
|
|
{
|
|
l->append(l, INT_MIN);
|
|
return;
|
|
}
|
|
|
|
if (n->left || n->right)
|
|
{
|
|
inorder(n->left, l);
|
|
l->append(l, n->val);
|
|
inorder(n->right, l);
|
|
}
|
|
else
|
|
l->append(l, n->val);
|
|
}
|
|
|
|
bool cmp(struct list_t l)
|
|
{
|
|
int s1 = 0, s2 = l.idx - 1;
|
|
|
|
for (; s1 < s2; s1++, s2--)
|
|
{
|
|
if (l.data[s1] != l.data[s2])
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool isSymmetric(struct TreeNode *root)
|
|
{
|
|
struct list_t l;
|
|
bool ret;
|
|
|
|
init_list(&l);
|
|
inorder(root, &l);
|
|
ret = cmp(l);
|
|
|
|
l.delete(&l);
|
|
return ret;
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef S2
|
|
#include <stdbool.h>
|
|
|
|
bool recur(struct TreeNode *l, struct TreeNode *r)
|
|
{
|
|
if (!l && !r)
|
|
return true;
|
|
if (!l || !r || l->val != r->val)
|
|
return false;
|
|
|
|
return recur(l->left, r->right) && recur(l->right, r->left);
|
|
}
|
|
|
|
bool isSymmetric(struct TreeNode *root)
|
|
{
|
|
return !root || recur(root->left, root->right);
|
|
}
|
|
#endif
|
|
|
|
int main()
|
|
{
|
|
return 0;
|
|
}
|