Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
C++:
bool isSameTree(TreeNode *p, TreeNode *q) {
if(!p)
{
if(!q)
{
return true;
}
else
{
return false;
}
}
if(p && !q)
{
return false;
}
if(p->val==q->val)
{
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
else
{
return false;
}
}
if(!p)
{
if(!q)
{
return true;
}
else
{
return false;
}
}
if(p && !q)
{
return false;
}
if(p->val==q->val)
{
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
else
{
return false;
}
}
没有评论:
发表评论