-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101.对称二叉树.php
168 lines (141 loc) · 3.75 KB
/
101.对称二叉树.php
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
<?php
/*
* @lc app=leetcode.cn id=101 lang=php
*
* [101] 对称二叉树
*
* https://leetcode-cn.com/problems/symmetric-tree/description/
*
* algorithms
* Easy (54.70%)
* Likes: 1378
* Dislikes: 0
* Total Accepted: 322.8K
* Total Submissions: 587.9K
* Testcase Example: '[1,2,2,3,4,4,3]'
*
* 给定一个二叉树,检查它是否是镜像对称的。
*
*
*
* 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
*
* 1
* / \
* 2 2
* / \ / \
* 3 4 4 3
*
*
*
*
* 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
*
* 1
* / \
* 2 2
* \ \
* 3 3
*
*
*
*
* 进阶:
*
* 你可以运用递归和迭代两种方法解决这个问题吗?
*
*/
// @lc code=start
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($val = 0, $left = null, $right = null) {
* $this->val = $val;
* $this->left = $left;
* $this->right = $right;
* }
* }
*/
class Solution
{
/**
* @param TreeNode $root
* @return Boolean
*/
public function isSymmetric($root)
{
// // 左边不等于右边,返回 false
// if (! ! $root->left === ! ! $root->right) {
// // 继续判断
// if ($root->left) {
// if ($root->left->val !== $root->right->val) {
// var_dump($root->left->val, $root->right->val);
// return false;
// }
// return $this->isSymmetric($root->left) && $this->isSymmetric($root->right);
// } else {
// return true;
// }
// } else {
// // 不同步
// return false;
// }
return $this->checkLayerIsInvalid([$root]);
}
public function checkLayerIsInvalid(array $roots)
{
$layer = count($roots);
// var_dump($layer);
// var_dump('roots. ', $roots);
// 遍历旧有的数据
// for ($i = 0, $j = $layer; $i <= $j; $i++, $j--) {
// if (! ! $roots[$i] !== ! ! $roots[$j]) {
// var_dump('here');
// return false;
// }
// if ($roots[$i] && $roots[$j]) {
// // 两边不相等,返回 false
// if ($roots[$i]->val !== $roots[$j]->val) {
// // var_dump($roots[$i]->val, $roots[$j]->val);
// return false;
// }
// }
// }
$val = [];
// 获取所有的数据
for ($i = 0 ; $i < $layer; $i++) {
$val[] = $roots[$i] ? $roots[$i]->val : null;
}
// [2,3,3,4,5,5,4,null,null,8,9,9,8]
// [1,2,2,null,3,null,3]
// [1,2,2,3,4,4,3]
for ($i = 0, $j = count($val) - 1; $i <= $j; $i++, $j--) {
if ($val[$i] !== $val[$j]) {
return false;
}
}
// var_dump($childrenCount, $layer);
$children = [];
$flag = 1; // 0, 不是全部都为 null, 1 全部都为 null
for ($i = 0; $i < $layer; $i++) {
$children[] = $roots[$i]->left;
$children[] = $roots[$i]->right;
if (! ! $roots[$i]->left || ! ! $roots[$i]->right) {
$flag = 0;
}
}
// var_dump($children, $layer, 2 ** $layer);
if ($flag === 1) {
// var_dump($children);
return true;
}
// var_dump('children count. ' . $childrenCount);
// var_dump('children. ', $children);
// 检查下一层
return $this->checkLayerIsInvalid($children);
}
}
// @lc code=end