100. Same Tree

递归验证 val 相等,处理下 leave node 的情况。

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not p and not q: 
            return True
        if p and q and p.val != q.val: 
            return False
        if p and q and p.val == q.val: 
            return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

Last updated

Was this helpful?