Lowest Common Ancestor in Binary Tree in DSA Python - Time & Space Complexity
We want to understand how the time needed to find the lowest common ancestor changes as the tree grows.
How does the search through the tree affect the time taken?
Analyze the time complexity of the following code snippet.
def lowestCommonAncestor(root, p, q):
if not root or root == p or root == q:
return root
left = lowestCommonAncestor(root.left, p, q)
right = lowestCommonAncestor(root.right, p, q)
if left and right:
return root
return left if left else right
This code finds the lowest common ancestor of two nodes in a binary tree by searching both sides recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Each node is visited once during the search.
As the tree grows, the function visits more nodes, roughly once per node.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The operations grow linearly with the number of nodes.
Time Complexity: O(n)
This means the time to find the ancestor grows directly with the number of nodes in the tree.
[X] Wrong: "The search only looks at part of the tree, so it is faster than visiting all nodes."
[OK] Correct: The function may need to check every node in the worst case, especially if the nodes are deep or on opposite sides.
Understanding this helps you explain how recursive tree searches work and why they take time proportional to tree size.
"What if the tree was a binary search tree and you used its properties? How would the time complexity change?"
Practice
Solution
Step 1: Understand the definition of LCA
The Lowest Common Ancestor is the deepest node in the tree that has both given nodes as descendants.Step 2: Compare options with definition
The deepest node that is an ancestor of both nodes matches the definition exactly. Other options describe different nodes.Final Answer:
The deepest node that is an ancestor of both nodes -> Option CQuick Check:
LCA = deepest common ancestor [OK]
- Confusing LCA with root node
- Thinking LCA is the smaller value node
- Assuming LCA is always the parent of one node
TreeNode?Solution
Step 1: Check parameter types and return type
The function should accept three TreeNode objects and return a TreeNode object representing the LCA.Step 2: Match with options
def find_lca(root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode: correctly uses type hints for parameters and return type. Others have wrong types or return types.Final Answer:
def find_lca(root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode: -> Option AQuick Check:
Correct signature uses TreeNode and returns TreeNode [OK]
- Using int instead of TreeNode for parameters
- Returning wrong type like int or str
- Omitting return type annotation
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def lowestCommonAncestor(root, p, q):
if not root or root == p or root == q:
return root
left = lowestCommonAncestor(root.left, p, q)
right = lowestCommonAncestor(root.right, p, q)
if left and right:
return root
return left if left else right
# Tree:
# 3
# / \
# 5 1
# / \ / \
# 6 2 0 8
# / \
# 7 4
root = TreeNode(3)
root.left = TreeNode(5)
root.right = TreeNode(1)
root.left.left = TreeNode(6)
root.left.right = TreeNode(2)
root.right.left = TreeNode(0)
root.right.right = TreeNode(8)
root.left.right.left = TreeNode(7)
root.left.right.right = TreeNode(4)
p = root.left # Node 5
q = root.left.right.right # Node 4
ancestor = lowestCommonAncestor(root, p, q)
print(ancestor.val)Solution
Step 1: Trace recursive calls for nodes 5 and 4
Node 5 is root.left, node 4 is root.left.right.right. The LCA is the lowest node that has both as descendants. Node 5 is an ancestor of node 4.Step 2: Identify the LCA node
Since 5 is ancestor of 4, LCA is node 5 itself.Final Answer:
5 -> Option DQuick Check:
LCA(5,4) = 5 [OK]
- Choosing root (3) instead of ancestor node
- Confusing node values with LCA
- Ignoring that node can be ancestor of itself
def lowestCommonAncestor(root, p, q):
if not root:
return None
if root == p or root == q:
return root
left = lowestCommonAncestor(root.left, p, q)
right = lowestCommonAncestor(root.right, p, q)
if left and right:
return root
if left:
return left
else:
return right
# Bug: The function sometimes returns None even if both nodes exist in tree.Solution
Step 1: Analyze base cases and recursive returns
The function returns None if root is None, and returns root if root matches p or q. It then searches left and right subtrees.Step 2: Identify missing check for node existence
If one node is not in the tree, the function may return None incorrectly. It does not verify both nodes exist before returning LCA.Final Answer:
The function does not check if both nodes exist in the tree before returning -> Option BQuick Check:
Missing node existence check causes None returns [OK]
- Assuming both nodes always exist in tree
- Changing base case to return root when root is None
- Returning root incorrectly when both left and right found
p and q given only their values (not references)?Solution
Step 1: Understand problem with duplicate values
When values are duplicated, searching by value alone can find multiple nodes, so LCA must consider all matching nodes.Step 2: Modify approach to handle duplicates
Find all nodes matching p and q values, then compute LCA for each pair to find the correct lowest common ancestor.Final Answer:
Modify the function to find all nodes with given values and find LCA of all pairs -> Option AQuick Check:
Handle duplicates by checking all matching nodes [OK]
- Using only first found node causing wrong LCA
- Assuming node references are always available
- Returning root node incorrectly for duplicates
Ready to prove it?
Pass the test and this pattern moves unknown
