# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param p TreeNode类 # @param q TreeNode类 # @return bool布尔型 # class Solution: def isSameTree(self , p , q ): if not q and not p: return True if (not q and p) or (q and not p): return False if q.val != p.val : return False left = self.isSameTree(p.left, q.left) right = self.isSameTree(p.right, q.right) return left and right # write code here
给出两个二叉树,请写出一个判断两个二叉树是否相等的函数。
判断两个二叉树相等的条件是:两个二叉树的结构相同,并且相同的节点上具有相同的值
使用递归即可解决