From cd37799b72126f0365358375133e460bbb1411b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=A4=E9=9C=B2?= <080301087@163.com> Date: Thu, 8 Jul 2021 16:04:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20-=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=86python3=E7=89=88=E6=9C=AC=E7=9A=84=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E9=80=92=E5=BD=92=E6=B3=95=E5=92=8C=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0098.验证二叉搜索树.md | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index b93d8cd5..4652fb86 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -343,7 +343,7 @@ Python: # self.val = val # self.left = left # self.right = right -//递归法 +# 递归法 class Solution: def isValidBST(self, root: TreeNode) -> bool: res = [] //把二叉搜索树按中序遍历写成list @@ -355,6 +355,35 @@ class Solution: return res buildalist(root) return res == sorted(res) and len(set(res)) == len(res) //检查list里的数有没有重复元素,以及是否按从小到大排列 + +# 简单递归法 +class Solution: + def isValidBST(self, root: TreeNode) -> bool: + def isBST(root, min_val, max_val): + if not root: return True + if root.val >= max_val or root.val <= min_val: + return False + return isBST(root.left, min_val, root.val) and isBST(root.right, root.val, max_val) + return isBST(root, float("-inf"), float("inf")) + +# 迭代-中序遍历 +class Solution: + def isValidBST(self, root: TreeNode) -> bool: + stack = [] + cur = root + pre = None + while cur or stack: + if cur: # 指针来访问节点,访问到最底层 + stack.append(cur) + cur = cur.left + else: # 逐一处理节点 + cur = stack.pop() + if pre and cur.val <= pre.val: # 比较当前节点和前节点的值的大小 + return False + pre = cur + cur = cur.right + return True + ``` Go: ```Go From 3ab914332ebfe60020061d2bc4bd9102687f4841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=A4=E9=9C=B2?= <080301087@163.com> Date: Thu, 8 Jul 2021 17:02:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=B7=AE.md=20-=20=E5=A2=9E=E5=8A=A0=E4=BA=86python3=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E8=BF=AD=E4=BB=A3=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 47b2b434..bf646443 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -222,6 +222,26 @@ class Solution: for i in range(len(res)-1): // 统计有序数组的最小差值 r = min(abs(res[i]-res[i+1]),r) return r + +# 迭代法-中序遍历 +class Solution: + def getMinimumDifference(self, root: TreeNode) -> int: + stack = [] + cur = root + pre = None + result = float('inf') + while cur or stack: + if cur: # 指针来访问节点,访问到最底层 + stack.append(cur) + cur = cur.left + else: # 逐一处理节点 + cur = stack.pop() + if pre: # 当前节点和前节点的值的差值 + result = min(result, cur.val - pre.val) + pre = cur + cur = cur.right + return result + ``` Go: > 中序遍历,然后计算最小差值