classSolution {publicbooleanisSameTree(TreeNode p,TreeNode q) {// p and q are both nullif (p ==null&& q ==null) {returntrue; }// one of p and q is null (note here p and q can not be both null, because if so it is returned above)if (p ==null|| q ==null) {returnfalse; }// p and q are not null, check if they have the same valueif (p.val!=q.val) {returnfalse; }returnisSameTree(p.left,q.left)&&isSameTree(p.right,q.right); }}