判断两个树是否相同,注意判断是否为NULL。

代码:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p==NULL || q==NULL)
{
if(p==NULL && q==NULL)
return true;
else
return false;
}
if(p->val==q->val)
{
return isSameTree(p->left, q->left)&& isSameTree(p->right, q->right);
}
return false;
}
网友评论