1class Solution {2public boolean isSameTree(TreeNode p, TreeNode q) {3// Base case: if both trees are null, they are identical4if (p == null && q == null) {5return true;6}7// If only one tree is null or the values are different, they are not identical8if (p == null || q == null || p.val != q.val) {9return false;10}11// Recursively check if the left and right subtrees are identical12return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);13}14}