O37
Lowest Common Ancestor in Tree
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) -> intInput & 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 benzersizdirExample 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: 5Example Test Scenarios (3)
Example 1
Input:
[[3,5,1,6,2,0,8,null,null,7,4],5,1]Expected Output:
3Example 2: Kendisi Ata
Input:
[[3,5,1,6,2,0,8,null,null,7,4],5,4]Expected Output:
5Example 3: İki Düğüm
Input:
[[1,2],1,2]Expected Output:
1