100. 相同的树





class Solution(object):
    def isSameTree(self, p, q):
        """
        :type p: TreeNode
        :type q: TreeNode
        :rtype: bool
        """
        # 根节点值不同,树不同
        if p.val != q.val:
            return False
        # 左右子树有一个不同,树不同
        if not p or not q:
            return False
        # 根节点值相同且左右子树均为空,树相同
        if not p and not q:
            return True
        # 递归判断左右子树
        return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
原文地址:https://www.cnblogs.com/panweiwei/p/13065366.html