-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101-Symmetric-Tree.php
39 lines (34 loc) · 981 Bytes
/
101-Symmetric-Tree.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
/**
* 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
*/
function isSymmetric($root) {
if ($root->left === null && $root->right === null) {
return true;
}
if ($root->left->val !== $root->right->val) {
return false;
}
$leftRoot = clone $root;
$leftRoot->left = $root->left->left;
$leftRoot->right = $root->right->right;
$rightRoot = clone $root;
$rightRoot->left = $root->left->right;
$rightRoot->right = $root->right->left;
return $this->isSymmetric($leftRoot) && $this->isSymmetric($rightRoot);
}
}