Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 43f30b2

Browse files
committed
修复 0235.二叉搜索树的最近公共祖先.md Python3解法
1 parent c41ce18 commit 43f30b2

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

problems/0235.二叉搜索树的最近公共祖先.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,15 @@ class Solution {
260260
递归法:
261261
```python
262262
class Solution:
263+
"""二叉搜索树的最近公共祖先 递归法"""
264+
263265
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
264-
if not root: return root //
265-
if root.val >p.val and root.val > q.val:
266-
return self.lowestCommonAncestor(root.left,p,q) //
267-
elif root.val < p.val and root.val < q.val:
268-
return self.lowestCommonAncestor(root.right,p,q) //
269-
else: return root
266+
if root.val > p.val and root.val > q.val:
267+
return self.lowestCommonAncestor(root.left, p, q)
268+
if root.val < p.val and root.val < q.val:
269+
return self.lowestCommonAncestor(root.right, p, q)
270+
return root
271+
270272
```
271273

272274
迭代法:

0 commit comments

Comments
 (0)