设一个比较函数比较左右节点就行。
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:bool isSymmetric(TreeNode* root) {if(root==nullptr) return 1;return issame(root->right,root->left);}bool issame(TreeNode* r,TreeNode* l){if(r&&l) return r->val==l->val?issame(r->right,l->left)&&issame(l->right,r->left):0;else return r||l?0:1;}
};