对称二叉树:
PHP 16ms:
100%。用上一题进行递归,位置换一下就行。
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {
/**
* @param TreeNode $root
* @return Boolean
*/
function isSymmetric($root) {
if($root == null){
return true;
}
return $this->isSameTree($root->left,$root->right);
}
function isSameTree($l,$r){
if($l == null && $r == null){
return true;
}
if($l->val == $r->val){
return $this->isSameTree($l->left,$r->right)&&$this->isSameTree($l->right,$r->left);
}else{
return false;
}
}
}