235. Lowest Common Ancestor of a Binary Search Tree
Leetcode Blind 75
29th Sept 2022 ~ Dion Pinto
Description
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” (Problem)
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':
curr = root
while curr:
if p.val>curr.val and q.val>curr.val:
curr = curr.right
elif p.val<curr.val and q.val<curr.val:
curr = curr.left
else:
return curr
Time Complexity => o(log(n))
Space Complexity => o(1)