Tuesday, August 18, 2015

Leetcode 100. Same Tree

https://leetcode.com/problems/same-tree/

Solution:
# T:O(n) S:O(h)
class Solution:
    # @param p, a tree node
    # @param q, a tree node
    # @return a boolean
    def isSameTree(self, p, q):
        if p is None and q is None:
            return True
        
        if p is not None and q is not None:
            return p.val == q.val and self.isSameTree(p.left, q.left)\
          and self.isSameTree(p.right, q.right)
        
        return False
Run Time: 60 ms

No comments:

Post a Comment