给你一个二叉树的根节点 root , 检查它是否轴对称。
示例1:
输入:root = [1,2,2,3,4,4,3] 输出:true
示例2:
输入:root = [1,2,2,null,3,null,3] 输出:false2、基础框架
class Solution {
public:
bool isSymmetric(TreeNode* root) {
}
};
3、原题链接
101. 对称二叉树
二、解题报告 1、思路分析(1)递归判断对称的节点是否相同
2、代码详解
class Solution {
public:
bool isMirror(TreeNode *n1, TreeNode *n2) {
if (n1 == nullptr ^ n2 == nullptr) return false;
if (n1 == nullptr && n2 == nullptr) return true;
return n1->val == n2->val && isMirror(n1->left, n2->right) && isMirror(n1->right, n2->left);
}
bool isSymmetric(TreeNode* root) {
return isMirror(root, root); //头结点不会影响对称关系
}
};



