Skip to content

Commit

Permalink
新增 0235.二叉搜索树的最近公共祖先.md Python3迭代法
Browse files Browse the repository at this point in the history
  • Loading branch information
RyouMon committed Sep 18, 2021
1 parent 43f30b2 commit 08199d5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion problems/0235.二叉搜索树的最近公共祖先.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,22 @@ class Solution:
if root.val < p.val and root.val < q.val:
return self.lowestCommonAncestor(root.right, p, q)
return root

```

迭代法:
```python
class Solution:
"""二叉搜索树的最近公共祖先 迭代法"""

def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
while True:
if root.val > p.val and root.val > q.val:
root = root.left
elif root.val < p.val and root.val < q.val:
root = root.right
else:
return root
```

## Go

Expand Down

0 comments on commit 08199d5

Please sign in to comment.