lc100.相同的树

 

题目链接

遍历,一个简单题。这个算法的时间复杂度是O(min(m, n)),空间复杂度是O(min(m, n))。

python:

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not p and not q:
            return True
        elif not p or not q:
            return False
        elif p.val != q.val:
            return False
        else:
            return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)