101. 对称二叉树

101. 对称二叉树

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root==NULL) return true;
        return sychelp(root->left,root->right);
    }
    bool sychelp(TreeNode *l,TreeNode *r){
        if(!l&&!r) return true;
        return l&&r&&l->val==r->val&&sychelp(l->left,r->right)&&sychelp(l->right,r->left);
    }
};

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

bfs

class Solution {
public:
    bool isSymmetric(TreeNode* r) {
        if(r==NULL) return true;
        queue<TreeNode*> q;
        q.push(r->left);
        q.push(r->right);
        while(!q.empty()){
            TreeNode *L=q.front();
            q.pop();
            TreeNode *R=q.front();
            q.pop();
            if(!L&&!R) continue;
            if(!(L&&R)) return false;
            if(L->val!=R->val) return false;
            q.push(L->left);
            q.push(R->right);
            q.push(L->right);
            q.push(R->left);
        }
        return true;
    }
};
---------------------
作者:L_Aster
来源:CSDN
原文:https://blog.csdn.net/gl486546/article/details/79778605
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2019-07-28 10:30  天涯海角路  阅读(81)  评论(0)    收藏  举报