flowchart TD
A[Enter node] --> B{node is null p q}
B -- Yes --> C[Return node]
B -- No --> D[Recurse left]
D --> E[Recurse right]
E --> F{left and right exist}
F -- Yes --> G[Current node is LCA]
F -- No --> H[Return non null branch]
G --> I[Bubble up]
H --> I
sequenceDiagram
participant Root
participant L as left subtree
participant R as right subtree
Root->>L: find p q
Root->>R: find p q
L-->>Root: left result
R-->>Root: right result
Root-->>Root: combine to LCA
# [LCA 템플릿: 아키텍트 버전]# Use Case: 두 노드의 공통 조상 찾기# Components: Tree Structure, Parent Tracking# Constraint: 트리 구조 필수deflowest_common_ancestor(root,p,q):# 1. 베이스 케이스 (Base Case)ifnotrootorroot==porroot==q:returnroot# 2. 재귀 탐색 (Recursive Search)left=lowest_common_ancestor(root.left,p,q)right=lowest_common_ancestor(root.right,p,q)# 3. 판단 로직 (Decision Logic)ifleftandright:returnroot# 양쪽에 모두 있으면 현재 노드가 LCAreturnleftifleftelseright# 한쪽에만 있으면 그쪽 반환