[Leetcode][python]Same Tree/相同的樹

題目大意

判斷兩顆二叉樹是否徹底相同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

相關文章
相關標籤/搜索