# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classSolution: # @param p, a tree node # @param q, a tree node # @return a boolean defisSameTree(self, p, q): ifnot p andnot q: returnTrue ifnot p and q: returnFalse if p andnot q: returnFalse if p and q and p.val!=q.val: returnFalse return self.isSameTree(p.right,q.right) and self.isSameTree(p.left,q.left)