题目
给定两颗二叉树,判断这两颗二叉树是否相同;
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
实现
递归:判断根节点的值;判断左子树和右子树;
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);
}