给你一个二叉树的根节点 root
, 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3] 输出:true
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == nullptr) return true;
return check(root->left, root->right);
}
bool check(TreeNode* left, TreeNode* right)
{
if (left == nullptr || right == nullptr) return left == right;
if (left->val != right->val) return false;
return check(left->left, right->right) && check(left->right, right->left);
}
};