Given a tree with N number of nodes and E number of edges find the number of subtrees that can be formed with each node center and R as radius l, without repetition in tree structure
Input format
R
N
E
Edges line by line
Function was with:
static void find_subtrees(int node, Map<Integer, List<Integer>> inputTree, int R,int[] output) {}
Input :
1
7
6
1 2
1 3
1 4
1 5
2 6
2 7
Output:
3
Used dfs to solve to find the unique ways but got the ouput as 7
static void dfs(int node, Map<Integer, List<Integer>> adj, boolean[] visited, int R, int currentDepth, StringBuilder subtreeStructure) {
if (currentDepth > R) return;
visited[node] = true;
subtreeStructure.append("(").append(node);
for (int neighbor : adj.getOrDefault(node, new ArrayList<>())) {
if (!visited[neighbor]) {
dfs(neighbor, adj, visited, R, currentDepth + 1, subtreeStructure);
}
}
subtreeStructure.append(")");
}