题解 | #对称的二叉树#

对称的二叉树

https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    bool isSymmetrical(TreeNode* pRoot) {
        return a(pRoot,pRoot);
    }
    bool a(TreeNode *l,TreeNode *r){
        if(l==nullptr&&r==nullptr) return true;
    
    if(l==nullptr||r==nullptr||l->val!=r->val){
        return false;
    }
    return a(r->left,l->right)&&a(r->right,l->left);
    }

};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务