Same Tree
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 binary tree
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode *p, struct TreeNode *q) {
if(!p && !q) return true;
if(!p || !q) return false;
if(p->val != q->val)
{
return false;
}
else if (isSameTree(p->right,q->right) && isSameTree(p->left,q->left))
{
return true;
}
}