Skip to content
C

Lowest Common Ancestor

Mediumjavapythonccppjavascript

Given a binary tree with distinct node values and two values P and Q that both appear in the tree, find the value of their lowest common ancestor (the deepest node that has both P and Q in its subtree).

Input Format

The first line contains an integer M, the number of level-order tokens. The second line contains M space-separated tokens (integers or null). The third line contains two integers P and Q.

Output Format

Print the value of the lowest common ancestor.

Input (stdin)

Output

Run your code to see output here...