题解 | #相同的二叉树#

相同的二叉树

https://www.nowcoder.com/practice/5a3b2cf4211249c89d6ced7987aeb775

    bool isSameTree(TreeNode* root1, TreeNode* root2) {
        // write code here
        if(root1==NULL && root2==NULL)
            return true;
        
        bitset<1> Is1Exist=root1? 1:0;
        bitset<1> Is2Exist=root2? 1:0;
    
        //运用同或,来判断两棵子树是都存在还是都不存在。
        bitset<1> IsSameInExist=~(Is1Exist^Is2Exist);
        //如果同或的结果为0
        if(!IsSameInExist.any())
        {
            return false;
        }
        if(root1->val!=root2->val)
        {
            return false;
        }
        
        if( (isSameTree(root1->left, root2->left) && isSameTree(root1->right, root2->right)) ){
            return true;
        }else{
            return false;
        }
    }

全部评论

相关推荐

07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务