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.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
bool k=true;
if(!p)
{
if(!q) return true;
return false;
}
if( !q) return false;
if (p->val != q->val) return false;
k = isSameTree(p->left,q->left);
if(!k) return false;
k=isSameTree(p->right,q->right);
if(!k) return false;
return true;
}
递归查询子树是否相同即可,不同的条件剔除。