PS
[코드트리 Trail 6] 트리의 부모 노드 [Java]
kimyoungrok
2025. 3. 5. 01:03
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