-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101.对称二叉树.c
More file actions
109 lines (86 loc) · 2.54 KB
/
101.对称二叉树.c
File metadata and controls
109 lines (86 loc) · 2.54 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
/*
* @lc app=leetcode.cn id=101 lang=c
*
* [101] 对称二叉树
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
// 未解决
bool isSymmetric(struct TreeNode* root){
if (root == NULL)
{
return true;
}
struct TreeNode* cache_first[1000];
int lenf = 0;
struct TreeNode* cache_last[1000];
int lenl = 0;
bool sign = true;
cache_first[lenf++] = root;
while (true)
{
bool over_sign = true;
if (sign)
{
lenl = 0;
for(int i = 0; i < lenf; i++)
{
if (cache_first[i])
{
cache_last[lenl++] = cache_first[i]->left;
cache_last[lenl++] = cache_first[i]->right;
over_sign = false;
}
else
{
cache_last[lenl++] = NULL;
cache_last[lenl++] = NULL;
}
}
for (int i = 0; i < lenl/2; i++)
{
if ((cache_last[i] == NULL && cache_last[lenl - i - 1]) || (cache_last[i] && cache_last[lenl - i - 1] == NULL) || (cache_last[lenl - i - 1] && cache_last[i] && cache_last[i]->val != cache_last[lenl - i - 1]->val))
{
return false;
}
}
sign = false;
}
else
{
lenf = 0;
for(int i = 0; i < lenl; i++)
{
if(cache_first[i])
{
cache_last[lenf++] = cache_first[i]->left;
cache_last[lenf++] = cache_first[i]->right;
over_sign = false;
}
else
{
cache_last[lenf++] = NULL;
cache_last[lenf++] = NULL;
}
}
for (int i = 0; i < lenf/2; i++)
{
if ((cache_first[i] == NULL && cache_first[lenf - i - 1]) || (cache_first[i] && cache_first[lenf - i - 1] == NULL) || (cache_first[lenf - i - 1] && cache_first[i] && cache_first[i]->val != cache_first[lenf - i - 1]->val))
{
return false;
}
}
sign = true;
}
if (over_sign)
{
return true;
}
}
}