牛客OJ:对称二叉树

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ShellDawn/article/details/88910668

先构造一颗对称二叉树,再判断;

class Solution {
public:
    TreeNode* solve(TreeNode* b){
        if(b == NULL) return NULL;
        TreeNode* a = new TreeNode(b->val);;
        a->left = solve(b->right);
        a->right = solve(b->left);
        return a;
    }
    bool judge(TreeNode* a,TreeNode* b){
        if(a==NULL && b == NULL) return true;
        if(a == NULL || b == NULL) return false;
        if(a -> val != b -> val) return false;
        return judge(a->left,b->left) && judge(a->right,b->right);
    }
    bool isSymmetrical(TreeNode* pRoot)
    {
        TreeNode* newRoot = pRoot;
        newRoot = solve(pRoot);
        return judge(newRoot,pRoot);
    }
};

猜你喜欢

转载自blog.csdn.net/ShellDawn/article/details/88910668