Discuss
Pick One
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.
1.当两棵树都遍历到叶子结点,返回true
2.当两棵树当前值不同,返回false
3.两棵树当前值相同,需要继续对其左子树、右子树进行判断
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
return judge(p,q);
}
public boolean judge(TreeNode a,TreeNode b){
if(a==null&&b==null)
return true;
if((a==null&&b!=null)||(a!=null&&b==null))
return false;
boolean value = (a.val==b.val);
return value&& judge(a.left,b.left)&&judge(a.right,b.right);
}
}