给出两个二叉树,请写出一个判断两个二叉树是否相等的函数。
判断两个二叉树相等的条件是:两个二叉树的结构相同,并且相同的节点上具有相同的值。
数据范围:树上的节点数满足
, 树上节点的值满足 
进阶: 空间复杂度
, 时间复杂度 )
进阶: 空间复杂度
/*
递归求解。
当前节点相等,左子树,右子树也相等,则认为这两个树是相等的。
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
if (p.val != q.val) return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
//非递归实现,主要思路是用两个队列进行层次遍历
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if(p==NULL && q==NULL)
return true;
queue<TreeNode*> q1,q2;
q1.push(p);q2.push(q);
TreeNode *tmp1,*tmp2;
while(!q1.empty()&&!q2.empty())
{
tmp1 = q1.front();
tmp2 = q2.front();
q1.pop();q2.pop();
if(tmp1==NULL && tmp2==NULL)
continue;
if(tmp1==NULL || tmp2==NULL)
return false;
if(tmp1->val!=tmp2->val)
return false;
if(tmp1!=NULL)
{
q1.push(tmp1->left);
q1.push(tmp1->right);
}
if(tmp2!=NULL)
{
q2.push(tmp2->left);
q2.push(tmp2->right);
}
}
if(!q1.empty()||!q2.empty())
return false;
return true;
}
};
/**
*
* @param p TreeNode类
* @param q TreeNode类
* @return bool布尔型
*/
public boolean isSameTree (TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}else if (p == null || q == null) {
return false;
}else if (p.val != q.val) {
return false;
}
// 两个数均不为null, 且 val值相等,直接递归比较下一层
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
} class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null){
return true;
}
if((p == null && q != null)||(p != null && q == null)){
return false;
}
//不要忘记判断节点的值哦~
if(p.val !=q.val){
return false;
}
return isSameTree(p.right,q.right) && isSameTree(p.left, q.left) ;
}
} class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if(p == NULL && q == NULL)
return true;
else if(p == NULL || q == NULL)
return false;
else if(p->val == q->val)
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
return false;
}
}; /**
* 两棵树是否是同一棵树
* 递归解法
* @param p
* @param q
* @return
*/
public boolean isSameTree_2(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree_2(p.left, q.left) && isSameTree_2(p.right, q.right);
}
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null) return true;
if((p == null && q != null) || (p != null && q == null)) return false;
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q==null)
return true;
if(q!=null || p==null)
return false;
return p.val==q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if (p == NULL or q == NULL) { return p == q; }
if (p->val != q->val) { return false; }
return isSameTree(p->left, q->left) and isSameTree(p->right, q->right);
}
};