100. Same Tree

Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

Example:

Input: p = [1,2], q = [1,null,
Output: false
def isSameTree(p, q):
       
    def recurse(n1, n2):       
        if n1 is None and n2 is None:
            return True
        if n1 is None or n2 is None:
            return False
       
        if n1.val == n2.val:
            return recurse(n1.left, n2.left) and recurse(n1.right, n2.right)
       
        return False
   

    return recurse(p, q)

Comments

Popular posts from this blog

849. Maximize Distance to Closest Person

347. Top K Frequent Elements

139. Word Break