https://school.programmers.co.kr/learn/courses/30/lessons/92343
왼쪽방문 하고 오른쪽 방문
오른쪽 방문하고 왼쪽방문 경우를 나눠서 구현해야 하나 고민하다가
찾아보고 말았다..
Node 리스트에 추가하면서 탐색하고 있다
import java.util.*;
class Solution {
int maxsheep = 0;
public int solution(int[] info, int[][] edges) {
List<Integer>[] tree = new List[info.length];
for(int i = 0; i < info.length; ++i)
tree[i] = new ArrayList<>();
for(int i = 0; i < edges.length; ++i){
int a = edges[i][0];
int b = edges[i][1];
tree[a].add(b);
}
List<Integer> next = new ArrayList<>();
next.add(0);
dfs(info, tree, next, 0, 0, 0);
return maxsheep;
}
public void dfs(int[] info, List<Integer>[] tree, List<Integer> Node, int root, int sheep, int wolf){
if(info[root] == 0)
{
sheep++;
}
else
wolf++;
if(wolf >= sheep)
return;
if(maxsheep < sheep)
maxsheep = sheep;
List<Integer> next = new ArrayList<>(Node);
if(tree[root].isEmpty() == false){
next.addAll(tree[root]);
}
next.remove(Integer.valueOf(root));
for(int n : next){
dfs(info, tree, next, n, sheep, wolf);
}
}
}
잘 봤습니다. 좋은 글 감사합니다.