Lowest Common Ancestor of a Binary Tree
Problem
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes
p
andq
as the lowest node inT
that has bothp
andq
as descendants (where we allow a node to be a descendant of itself).”
Example 1:
Example 2:
Example 3:
Pseudocode
Solution
Time and Space Complexity
Time
What did the code do
Total -
Space
What did the code do
Total -
Last updated