Back to Practice Dashboard
DSA SectionEasy
LCA
Learn how to solve the 'LCA' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function find_lca(tree_arr, p, q) that takes an array representation of a binary tree tree_arr and two node values p and q, and returns the value of their Lowest Common Ancestor (LCA).
Constraints
- •2 <= len(tree_arr) <= 1000
- •p and q are present in the tree
Examples
Example 1
Input
tree_arr = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], p = 5, q = 1
Output
3
Explanation
The LCA of nodes 5 and 1 is 3.
Example 2
Input
tree_arr = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], p = 5, q = 4
Output
5
Explanation
The LCA of nodes 5 and 4 is 5, since a node can be an ancestor of itself.
Need a Hint?
Perform a recursive tree traversal (DFS) or level-order traversal (BFS) using a queue/stack.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.