Skip to content

Latest commit

 

History

History
23 lines (19 loc) · 544 Bytes

91alg_day14_lc100.md

File metadata and controls

23 lines (19 loc) · 544 Bytes

Idea

Traverse two trees together, compare their current nodes.

Code

    def isSameTree(self, p, q):
        if p == None and q == None:
            return True
        if p == None and q!= None:
            return False
        if p!= None and q == None:
            return False
        if (p.val==q.val):
            return True and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
        else:
            return False

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)