递归
public boolean isSameTree(TreeNode p, TreeNode q) { // p、q都为空 if (p == null && q == null) return true; // p、q有一个为空 if (p == null || q == null) return false; return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); }
public boolean isSameTree(TreeNode p, TreeNode q) { // p、q都为空 if (p == null && q == null) return true; // p、q有一个为空 if (p == null || q == null) return false; return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); }