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
▲ Console

Install Talent Arabia

Get instant access to jobs and career tools on your device.