| Total Complexity | 17 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class SymmetricTree |
||
| 10 | { |
||
| 11 | public static function isSymmetric(?TreeNode $root): bool |
||
| 12 | { |
||
| 13 | if (!$root) { |
||
| 14 | return false; |
||
| 15 | } |
||
| 16 | $stack = []; |
||
| 17 | array_push($stack, $root->left); |
||
| 18 | array_push($stack, $root->right); |
||
| 19 | while ($stack) { |
||
| 20 | [$p, $q] = [array_shift($stack), array_shift($stack)]; |
||
| 21 | if (!$p && !$p) { |
||
| 22 | continue; |
||
| 23 | } |
||
| 24 | if (!$p || !$q || $p->val !== $q->val) { |
||
| 25 | return false; |
||
| 26 | } |
||
| 27 | array_push($stack, $p->left); |
||
| 28 | array_push($stack, $q->right); |
||
| 29 | array_push($stack, $p->right); |
||
| 30 | array_push($stack, $q->left); |
||
| 31 | } |
||
| 32 | |||
| 33 | return true; |
||
| 34 | } |
||
| 35 | |||
| 36 | public static function isSymmetric2(?TreeNode $root): bool |
||
| 37 | { |
||
| 38 | if (!$root) { |
||
| 39 | return false; |
||
| 40 | } |
||
| 41 | |||
| 42 | return self::helper($root->left, $root->right); |
||
| 43 | } |
||
| 44 | |||
| 45 | private static function helper(?TreeNode $p, ?TreeNode $q): bool |
||
| 57 | } |
||
| 58 | } |
||
| 59 |