Lowest Common Ancestor of BST

Binary Search Tree
Easy

Approach

Diagrams

Analysis

Time

O(n)

Space

O(1)
class Solution:
    def lowestCommonAncestor(self, root, node, otherNode):
        while root:
            if root.val > node.val and root.val > otherNode.val:
                root = root.left

            elif root.val < node.val and root.val < otherNode.val:
                root = root.right

            else:
                return root

Learn

Videos