【问题描述】
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 static boolean isSameTree(TreeNode100 p, TreeNode100 q) {
if (p == null && q == null) {
return true;
}
if (p == null && q != null) {
return false;
}
if (p != null && q == null) {
return false;
}
return p.val == q.val && isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}