최소 공통 조상(LCA)
Last updated
Last updated
[depth : 정점]
0 → 1(root 정점)
1 → 2, 3
2 → 4, 5, 6, 7// 1 ~ 7번 정점 (root는 부모가 없기 때문에 0)
int parent[] = {0, 1, 1, 2, 2, 3, 3}// 두 정점의 depth 확인하기
while(true){
if(depth가 일치)
if(두 정점의 parent 일치?) LCA 찾음(종료)
else 두 정점을 자신의 parent 정점 값으로 변경
else // depth 불일치
더 depth가 깊은 정점을 해당 정점의 parent 정점으로 변경(depth가 감소됨)
}