Lowest Common Ancestor
Medium
Trees
Tree
DFS
Find LCA of two nodes p and q in binary tree.
Constraints
Nodes ≤ 10^5
Examples
Example 1:
Input: 3 5 1 6 2 0 8 null null 7 4
5
1
Output: 3
Example 2:
Input: 3 5 1 6 2 0 8 null null 7 4
5
4
Output: 5
Tests:
Runtime:
Memory:
Test
Input:
Expected:
Got:
Click Run Code to test against sample cases, or Submit to test against all cases.
▲ Console