Skip to main content

235 - Lowest Common Ancestor of a Binary Search Tree

Details

KeyValue
Linkhttps://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
LanguagePython 3
Runtime122 ms, faster than 44.61% of Python3 online submissions for Lowest Common Ancestor of a Binary Search Tree
Memory Usage18.9 MB, less than 22.45% of Python3 online submissions for Lowest Common Ancestor of a Binary Search Tree
DatastructuresList[str]
AlgorithmsIteration
ComplexityTime: O(N) Memory: O(1)

Procedure

  1. ...

Code

# 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':
while root:
if p.val < root.val > q.val: root = root.left
elif p.val > root.val < q.val: root = root.right
else: return root