1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
class Solution: def DoesTree1HaveTree2(self, pRoot1, pRoot2): if not pRoot2: return True if not pRoot1: return False if pRoot1.val != pRoot2.val: return False return self.DoesTree1HaveTree2(pRoot1.left, pRoot2.left) and self.DoesTree1HaveTree2(pRoot1.right, pRoot2.right) def HasSubtree(self, pRoot1, pRoot2): result = False if pRoot1 and pRoot2: if pRoot1.val == pRoot2.val: result = self.DoesTree1HaveTree2(pRoot1, pRoot2) if not result: result = self.HasSubtree(pRoot1.left, pRoot2) if not result: result = self.HasSubtree(pRoot1.right, pRoot2) return result
|