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;
}
}

京公网安备 11010502036488号