So, the lowest common ancestor for any two nodes (p
and q
) in a BST is termed as the minimum value node that contains both p
and q
in their sub trees. The lowest common ancestor for any node is itself or some previous node.
Code:
Java
Big O Analysis
-
Runtime
The runtime complexity here is
O(N)
. -
Memory
The memory usage is
O(N)
since we would use the implicit call stack.
— A