每日一题力扣 100 相同的树

给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/same-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not p and not q:return True
        if not p and q:return False
        if not q and p:return False
        if p.val!=q.val:return False
        return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
原文地址:https://www.cnblogs.com/liuxiangyan/p/14601079.html