Lowest Common Ancestor of a Binary Search Tree

Solution Code
class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        current = root
        while current:
            if p.val > current.val and q.val > current.val:
                current = current.right
            elif p.val < current.val and q.val < current.val:
                current = current.left
            else:
                return current
Video GuideLeetcode Daily

Time Complexity

O(N)

Space Complexity

O(1) Iterative