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.
判断两个树是否完全相同,判断的标准是每个节点的值是否相等,若相等的话,其左右节点是否相同,然后用递归调用这个函数,对节点的左子树和右子树继续判断即可,(首先判断一下两颗树是否都为空),代码如下:
if(p==null&&q==null){return true;}
if(p==null||q==null){return false;}
if(p.val==q.val){
return isSameTree(p.left,q.left)&&isSameTree(p.right, q.right);
}
return false;