νΈλ¦¬ μκ³ λ¦¬μ¦Intermediate
μ΅μ κ³΅ν΅ μ‘°μ (LCA)
μ¬κ·μ μ κ·Ό λ°©μμ μ¬μ©νμ¬ μ΄μ§ νΈλ¦¬μμ λ λ Έλμ μ΅μ κ³΅ν΅ μ‘°μμ μ°Ύμ΅λλ€. LCAλ λ λ Έλλ₯Ό λͺ¨λ μμμΌλ‘ κ°λ κ°μ₯ κΉμ λ Έλμ λλ€. κ³μΈ΅μ λ°μ΄ν°(κ΄κ³ μ°ΎκΈ°), λ Έλ κ° κ±°λ¦¬ κ³μ°, λ²μ κ΄λ¦¬ μμ€ν (λ³ν© λ² μ΄μ€)μ μμ©μ΄ μμ΅λλ€. λ κ³ κΈ νΈλ¦¬ 쿼리μ κΈ°μ΄μ λλ€.
#tree#binary-tree#recursion#ancestry
Complexity Analysis
Time (Average)
O(n)Expected case performance
Space
O(h)Memory requirements
Time (Best)
O(n)Best case performance
Time (Worst)
O(n)Worst case performance
Use 'null' for empty nodes
How it works
- β’ Recursively traverse the tree
- β’ Find nodes p and q in subtrees
- β’ LCA is where both are found
- β’ O(n) time, O(h) space
- β’ Used in version control systems
Step: 1 / 0
500ms
SlowFast
Keyboard Shortcuts
Space Play/Pauseβ β StepR Reset1-4 Speed
Real-time Statistics
Algorithm Performance Metrics
Progress0%
Comparisons
0
Swaps
0
Array Accesses
0
Steps
1/ 0
Algorithm Visualization
Step 1 of 0
Initialize array to begin
Default
Comparing
Swapped
Sorted
Code Execution
Currently executing
Previously executed
Implementation