Skip to content
CampusEduX

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.

Related Problems

  • Check Balanced Binary Tree
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Diameter of Binary Tree
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Zigzag Level Order Traversal
    Medium · java, python, c, cpp, javascript
    Solve Problem

Input (stdin)

Output

Run your code to see output here...