/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return bool布尔型
*/
bool isSymmetric(TreeNode* root) {
// write code here
if(!root) // 判定特例
return true;
return function(root->left, root->right);
}
bool function(TreeNode* left, TreeNode* right) {
if(!left && !right) // 当左右节点都为空时
return true;
if((!left || !right) || (left->val != right->val)) // 当左右节点有一个不为空,或者两者值不相等时
return false;
// 判断左节点的右节点和右节点的左节点,左节点的左节点和右节点的右节点
return function(left->right, right->left) && function(left->left, right->right);
}
};