数据结构:树🌲
时间复杂度:O(n)
空间复杂度:O(1)
代码实现:
class Solution:def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':while True:if p.val <= root.val <= q.val or q.val <= root.val <= p.val:return rootelif p.val > root.val and q.val > root.val:root = root.rightelse:root = root.left