728x90
문제
https://www.codetree.ai/trails/complete/curated-cards/intro-parent-node-of-the-tree/introduction
Code Tree | Learning to Code with Confidence
풀이
각 노드의 부모 노드를 구하는 문제다.
주어진 트리 정보에 대해 dfs를 하며 각 부모 노드를 구하면 된다.
private static void dfs(int node) {
for (int child : tree.get(node)) {
if (parent[child] == -1) {
parent[child] = node;
dfs(child);
}
}
}
소스코드
728x90
'PS' 카테고리의 다른 글
[Programmers 알고리즘 고득점 Kit] 여행 경로[Python] (0) | 2025.02.28 |
---|---|
[Programmers 알고리즘 고득점 Kit] 네트워크 [Python] (0) | 2025.02.26 |
[LeetCode] 197. Rising Temperature [MySQL] (0) | 2025.02.26 |
[프로그래머스] 카펫 [Java] (0) | 2025.02.21 |
[프로그래머스] 등굣길 [Java] (0) | 2025.02.01 |