0
0
DSA Cprogramming~10 mins

DP on Trees Diameter of Tree in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the adjacency list for the tree.

DSA C
int adj[[1]][100]; // adjacency list for tree
int size[[1]]; // size of adjacency for each node
Drag options to blanks, or click blank then click option'
A1000
B10000
C100
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing too small an array size causing out-of-bounds errors.
Choosing unnecessarily large arrays wasting memory.
2fill in blank
medium

Complete the code to add an edge from node u to node v in the adjacency list.

DSA C
adj[u][size[u]++] = [1];
Drag options to blanks, or click blank then click option'
Av
Bu
Csize[u]
Dsize[v]
Attempts:
3 left
💡 Hint
Common Mistakes
Adding u instead of v causing incorrect adjacency.
Using size[v] which is unrelated here.
3fill in blank
hard

Fix the error in the DFS function header to correctly pass the parent node.

DSA C
void dfs(int u, int [1]) {
Drag options to blanks, or click blank then click option'
Aparent
Bpar
Cprev
Dp
Attempts:
3 left
💡 Hint
Common Mistakes
Using unclear or inconsistent parameter names.
Omitting the parent parameter causing infinite recursion.
4fill in blank
hard

Fill both blanks to update the diameter and height during DFS.

DSA C
if (height[v] + height[u] + 1 > diameter) {
    diameter = height[v] + height[u] + [1];
}
height[u] = 1 + [2];
Drag options to blanks, or click blank then click option'
A1
B0
Cheight[v]
Ddiameter
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add 1 to diameter update.
Assigning height[u] without adding 1.
5fill in blank
hard

Fill all three blanks to complete the DFS loop over children and update heights.

DSA C
for (int i = 0; i < size[u]; i++) {
    int v = adj[u][[1]];
    if (v != [2]) {
        dfs(v, u);
        if (height[v] > height[u]) {
            height[u] = height[v];
        }
    }
}
height[u] = height[u] + [3];
Drag options to blanks, or click blank then click option'
Ai
Bparent
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index variable for adjacency.
Not skipping the parent node causing infinite recursion.
Forgetting to add 1 to height[u].