572. Subtree of Another Tree
來自專欄 leetcode_python刷題_easy題
class Solution: # https://leetcode.com/problems/subtree-of-another-tree/discuss/102741/Python-Straightforward-with-Explanation-(O(ST)-and-O(S+T)-approaches) # Merkle hashing的方法暫時先不了解,調用了庫函數面試也可能不讓用 def isSubtree(self, s, t): """ :type s: TreeNode :type t: TreeNode :rtype: bool """ if s is None: return False if self.isMatch(s, t): return True return self.isSubtree(s.left, t) or self.isSubtree(s.right, t) def isMatch(self, s, t): if s is None or t is None: return s is t return (s.val == t.val and self.isMatch(s.left, t.left) and self.isMatch(s.right, t.right))
推薦閱讀:
※我python已經入門,想更深入的學習python,但沒有什麼具體方向?
※深入理解 python 中的賦值、引用、拷貝
※Python | 爬蟲-數據分析實戰Ⅰ
※C++ 程序的圖形化界面運行,需要用到什麼工具?
TAG:LeetCode領扣 | Python |