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.
就是比较两棵树相同位置节点相同的情况下,同时它们对应位置的节点的左子树和右子树是不是相同的。
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null)
{
return true;
}
else if(p!=null&&q!=null)
{
boolean flag=false;
flag=((p.val==q.val)&&isSameTree(p.left,q.left)&&isSameTree(p.right,q.right));
return flag;
}else
return false;
}
}