链接:对称二叉树
    image.png
    思路:
    image.png
    此外,还需要考虑到节点但为空的问题。(p或者q都为空为true,p或者q任意一个为空为false)

    方法:递归

    1. class Solution {
    2. public:
    3. bool isSymmetric(TreeNode* root) {
    4. return isduicheng(root,root);
    5. }
    6. bool isduicheng(TreeNode* p,TreeNode* q){
    7. if(!p&&!q) return true;
    8. if(!p||!q) return false;
    9. //实现在树里面两个指针镜像移动
    10. //当p左移时q往右,p右q往左
    11. return p->val == q->val && isduicheng(p->left,q->right) && isduicheng(p->right,q->left);
    12. }
    13. };