mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #212 from jojoo15/patch-15
添加 0235.二叉搜索树的最近公共祖先 python版本
This commit is contained in:
@ -247,8 +247,23 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
# Definition for a binary tree node.
|
||||||
|
# class TreeNode:
|
||||||
|
# def __init__(self, x):
|
||||||
|
# self.val = x
|
||||||
|
# self.left = None
|
||||||
|
# self.right = None
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
|
||||||
|
if not root: return root //中
|
||||||
|
if root.val >p.val and root.val > q.val:
|
||||||
|
return self.lowestCommonAncestor(root.left,p,q) //左
|
||||||
|
elif root.val < p.val and root.val < q.val:
|
||||||
|
return self.lowestCommonAncestor(root.right,p,q) //右
|
||||||
|
else: return root
|
||||||
|
```
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user