From a8109b40141202dd9782f5389f0816c8057999c5 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sun, 1 Aug 2021 10:15:22 -0400 Subject: [PATCH 1/2] =?UTF-8?q?Update=200700.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改代码符合PEP 8 规范. --- problems/0700.二叉搜索树中的搜索.md | 21 +++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index d6899ac5..e6eb0358 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -212,17 +212,20 @@ Python: 递归法: ```python -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right class Solution: def searchBST(self, root: TreeNode, val: int) -> TreeNode: - if not root or root.val == val: return root //为空或者已经找到都是直接返回root,所以合并了 - if root.val > val: return self.searchBST(root.left,val) //注意一定要加return - else: return self.searchBST(root.right,val) + # 为什么要有返回值: + # 因为搜索到目标节点就要立即return, + # 这样才是找到节点就返回(搜索某一条边),如果不加return,就是遍历整棵树了。 + + if not root or root.val == val: + return root + + if root.val > val: + return self.searchBST(root.left, val) + + if root.val < val: + return self.searchBST(root.right, val) ``` From 2a94d6ca0f413d461537f112cfb8b99855b13cd5 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sun, 1 Aug 2021 10:17:03 -0400 Subject: [PATCH 2/2] =?UTF-8?q?Update=200700.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0700.二叉搜索树中的搜索.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index e6eb0358..4b02f8af 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -212,6 +212,12 @@ Python: 递归法: ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def searchBST(self, root: TreeNode, val: int) -> TreeNode: # 为什么要有返回值: