判斷兩顆二叉樹是否徹底相同python
簡單題,一開始思考半天中序遍歷的解法,發現太繞。
其實應該就是先根節點,再左右,也就是前序遍歷。markdown
class Solution(object):
def isSameTree(self, p, q):
if p == None and q == None: return True
if p and q and p.val == q.val:
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
return False
前序中序後序遍歷,各有優點,多擴散思惟。ide