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?