O37

Lowest Common Ancestor in Tree

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: NUMERIC

Problem Statement & Objective

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

Function Signature (Python)

lowest_common_ancestor_in_tree(root: list, p: int, q: int) -> int

Input & Output Contract

Output Rule:LCA düğümünün tam sayı değeri.

System Constraints

2 <= agac.length <= 10^4
p != q ve ikisi de ağaçta mevcuttur
Tüm düğüm değerleri benzersizdir

Example Execution Walkthrough

agac = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], p = 5, q = 1 -> LCA: 3
p = 5, q = 4 -> 4 düğümü 5'in torunudur -> LCA: 5

Example Test Scenarios (3)

Example 1
Input:[[3,5,1,6,2,0,8,null,null,7,4],5,1]
Expected Output:3
Example 2: Kendisi Ata
Input:[[3,5,1,6,2,0,8,null,null,7,4],5,4]
Expected Output:5
Example 3: İki Düğüm
Input:[[1,2],1,2]
Expected Output:1
Loading Python Editor...
Input Parameters
[
  [
    3,
    5,
    1,
    6,
    2,
    0,
    8,
    null,
    null,
    7,
    4
  ],
  5,
  1
]
Expected Output
3