题目链接:https://www.acwing.com/problem/content/38/
题目如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
return dfs(root->left,root->right);
}
bool dfs(TreeNode* left,TreeNode* right){
if(left==NULL&&right==NULL) return true;
if(left==NULL&&right!=NULL) return false;
if(left!=NULL&&right==NULL) return false;
if(left->val!=right->val) return false;
return dfs(left->left,right->right)&&dfs(left->right,right->left);
}
};