python solution


  • 0
    H
    class Solution(object):
        def isSubtree(self, s, t):
            """
            :type s: TreeNode
            :type t: TreeNode
            :rtype: bool
            """
            def check (s,t):
                if s==None and t==None:
                    return True
                elif s==None or t==None:
                    return False
                l=check(s.left,t.left)
                r=check(s.right,t.right)
                if s.val==t.val and l and r:
                    return  True
                return False
    
            if check(s,t): return True
            if s.left and self.isSubtree(s.left,t): return True
            if s.right and self.isSubtree(s.right,t): return True
            return False

  • 0
    G

    FYI, it's best practice to use if s is None instead of if s==None. See the second bullet here.


  • 0
    H

    @GravityGrave
    thanks for point out


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.