From 3031745b1511dc94e96ef8115002e1b8e2b77add Mon Sep 17 00:00:00 2001 From: SunnyQjm Date: Sun, 28 Jun 2020 22:31:14 +0800 Subject: [PATCH] update --- chapter10/1_lowest-common-ancestor-of-a-binary-search-tree.py | 3 +-- chapter10/2_balanced-binary-tree.py | 3 +++ chapter10/3_binary-tree-paths.py | 3 ++- chapter11/6_maximum-depth-of-binary-tree.py | 1 + chapter11/7_minimum-depth-of-binary-tree.py | 1 + chapter9/3_my-calendar-i.py | 3 +-- chapter9/4_same-tree.py | 4 ++-- chapter9/5_path-sum.py | 2 +- chapter9/6_invert-binary-tree.py | 2 +- 9 files changed, 13 insertions(+), 9 deletions(-) diff --git a/chapter10/1_lowest-common-ancestor-of-a-binary-search-tree.py b/chapter10/1_lowest-common-ancestor-of-a-binary-search-tree.py index 002b9ee..f90b707 100644 --- a/chapter10/1_lowest-common-ancestor-of-a-binary-search-tree.py +++ b/chapter10/1_lowest-common-ancestor-of-a-binary-search-tree.py @@ -59,6 +59,7 @@ class Solution: 2. 如果找到一个节点,其值大于等于left.val,且小于等于right.val,则该节点即为要求的结果 3. 如果当前节点不是目标节点,则对左右子树进行递归遍历 """ + def preOrderTraversal(root, left, right): if not root or (left.val <= root.val <= right.val): return root @@ -70,5 +71,3 @@ class Solution: left = p if p.val < q.val else q right = p if p.val >= q.val else q return preOrderTraversal(root, left, right) - - diff --git a/chapter10/2_balanced-binary-tree.py b/chapter10/2_balanced-binary-tree.py index 0a6d50e..c7ad30b 100644 --- a/chapter10/2_balanced-binary-tree.py +++ b/chapter10/2_balanced-binary-tree.py @@ -31,6 +31,7 @@ # 返回 false 。 ####################################################################################### + class TreeNode: def __init__(self, x): self.val = x @@ -55,6 +56,7 @@ class Solution: 2. 根据AVL树的特性,如果任意一个节点的左右子树的高度差大于一,则该树不是AVL树,标记器高度为-1; 3. 如果某个子树的高度计算为-1,则表示该子树不是AVL树,-1会一直传递到顶层。 """ + def preOrderTraversal(root): if not root: return 0 @@ -63,4 +65,5 @@ class Solution: if leftNum == -1 or rightNum == -1 or abs(leftNum - rightNum) > 1: return -1 return 1 + max(leftNum, rightNum) + return preOrderTraversal(root) != -1 diff --git a/chapter10/3_binary-tree-paths.py b/chapter10/3_binary-tree-paths.py index e41d358..d955382 100644 --- a/chapter10/3_binary-tree-paths.py +++ b/chapter10/3_binary-tree-paths.py @@ -21,6 +21,7 @@ # 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3 ####################################################################################### + class TreeNode: def __init__(self, x): self.val = x @@ -44,6 +45,7 @@ class Solution: 1. 递归的进行先序遍历,过程中记录走过的路径; 2. 当遍历到一个叶子节点时,将当前路径添加到结果集当中; """ + def preorderTraversal(root, s, result): if not root: return @@ -60,4 +62,3 @@ class Solution: result = [] preorderTraversal(root, s, result) return result - diff --git a/chapter11/6_maximum-depth-of-binary-tree.py b/chapter11/6_maximum-depth-of-binary-tree.py index 38fed89..bbff841 100644 --- a/chapter11/6_maximum-depth-of-binary-tree.py +++ b/chapter11/6_maximum-depth-of-binary-tree.py @@ -19,6 +19,7 @@ # 返回它的最大深度 3 。 ####################################################################################### + class TreeNode: def __init__(self, x): self.val = x diff --git a/chapter11/7_minimum-depth-of-binary-tree.py b/chapter11/7_minimum-depth-of-binary-tree.py index c5eec44..4662d9e 100644 --- a/chapter11/7_minimum-depth-of-binary-tree.py +++ b/chapter11/7_minimum-depth-of-binary-tree.py @@ -19,6 +19,7 @@ # 返回它的最小深度 2. ####################################################################################### + class TreeNode: def __init__(self, x): self.val = x diff --git a/chapter9/3_my-calendar-i.py b/chapter9/3_my-calendar-i.py index e50f3dd..3502361 100644 --- a/chapter9/3_my-calendar-i.py +++ b/chapter9/3_my-calendar-i.py @@ -30,12 +30,11 @@ # param_1 = obj.book(start,end) class MyCalendar: - + def __init__(self): # 定义一个数组,用于记录行程 self.calendar = [] - def book(self, start, end): """ :type start: int diff --git a/chapter9/4_same-tree.py b/chapter9/4_same-tree.py index bfb848b..ad5af5c 100644 --- a/chapter9/4_same-tree.py +++ b/chapter9/4_same-tree.py @@ -35,6 +35,7 @@ # 输出: false ####################################################################################### + class TreeNode: def __init__(self, x): self.val = x @@ -58,7 +59,7 @@ class Solution: if not p or not q: if not q and not p: return True - else: + else: return False if p.val != q.val: @@ -83,4 +84,3 @@ if __name__ == '__main__': q = TreeNode(1) q.right = TreeNode(2) print(solution.isSameTree(p, q), "= False") - diff --git a/chapter9/5_path-sum.py b/chapter9/5_path-sum.py index 8734a18..26670d9 100644 --- a/chapter9/5_path-sum.py +++ b/chapter9/5_path-sum.py @@ -28,6 +28,7 @@ class TreeNode: self.left = None self.right = None + class Solution: def hasPathSum(self, root, sum): """ @@ -64,4 +65,3 @@ if __name__ == '__main__': root.right.right = TreeNode(4) root.right.right.right = TreeNode(1) print(solution.hasPathSum(root, 22), "= True") - diff --git a/chapter9/6_invert-binary-tree.py b/chapter9/6_invert-binary-tree.py index 710c513..eacfbc8 100644 --- a/chapter9/6_invert-binary-tree.py +++ b/chapter9/6_invert-binary-tree.py @@ -54,7 +54,7 @@ class Solution: if not root: return None root.left, root.right = root.right, root.left - + self.invertTree(root.left) self.invertTree(root.right) return root