0
0
DSA Cprogramming~20 mins

DP on Trees Diameter of Tree in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tree Diameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of diameter calculation on a simple tree
What is the output of the following C code that calculates the diameter of a tree represented as an adjacency list?
DSA C
#include <stdio.h>
#include <stdlib.h>

#define MAX 5

int adj[MAX][MAX] = {0};
int visited[MAX] = {0};
int max_diameter = 0;

int dfs(int node) {
    visited[node] = 1;
    int max1 = 0, max2 = 0;
    for (int i = 0; i < MAX; i++) {
        if (adj[node][i] && !visited[i]) {
            int dist = dfs(i) + 1;
            if (dist > max1) {
                max2 = max1;
                max1 = dist;
            } else if (dist > max2) {
                max2 = dist;
            }
        }
    }
    if (max1 + max2 > max_diameter) {
        max_diameter = max1 + max2;
    }
    return max1;
}

int main() {
    adj[0][1] = adj[1][0] = 1;
    adj[1][2] = adj[2][1] = 1;
    adj[1][3] = adj[3][1] = 1;
    adj[3][4] = adj[4][3] = 1;

    dfs(0);
    printf("%d\n", max_diameter);
    return 0;
}
A4
B3
C5
D2
Attempts:
2 left
💡 Hint
Think about the longest path between any two nodes in the tree.
🧠 Conceptual
intermediate
1:30remaining
Understanding the diameter calculation logic
In the DP approach to find the diameter of a tree, what does the sum of the two largest depths from a node represent?
AThe height of the tree
BThe total number of nodes in the tree
CThe longest path passing through that node
DThe number of leaf nodes
Attempts:
2 left
💡 Hint
Think about how the diameter can pass through a node connecting two longest branches.
Predict Output
advanced
2:30remaining
Output of diameter calculation with weighted edges
What is the output of the following C code that calculates the diameter of a weighted tree using DP on trees?
DSA C
#include <stdio.h>
#include <stdlib.h>

#define MAX 4

int adj[MAX][MAX] = {0};
int visited[MAX] = {0};
int max_diameter = 0;

int dfs(int node) {
    visited[node] = 1;
    int max1 = 0, max2 = 0;
    for (int i = 0; i < MAX; i++) {
        if (adj[node][i] > 0 && !visited[i]) {
            int dist = dfs(i) + adj[node][i];
            if (dist > max1) {
                max2 = max1;
                max1 = dist;
            } else if (dist > max2) {
                max2 = dist;
            }
        }
    }
    if (max1 + max2 > max_diameter) {
        max_diameter = max1 + max2;
    }
    return max1;
}

int main() {
    adj[0][1] = adj[1][0] = 3;
    adj[1][2] = adj[2][1] = 4;
    adj[1][3] = adj[3][1] = 2;

    dfs(0);
    printf("%d\n", max_diameter);
    return 0;
}
A9
B6
C5
D7
Attempts:
2 left
💡 Hint
Sum the two longest weighted paths from the root node.
🔧 Debug
advanced
2:00remaining
Identify the bug in diameter calculation code
What error will the following code produce when calculating the diameter of a tree?
DSA C
#include <stdio.h>
#include <stdlib.h>

#define MAX 3

int adj[MAX][MAX] = {0};
int visited[MAX] = {0};
int max_diameter = 0;

int dfs(int node) {
    visited[node] = 1;
    int max1 = 0, max2 = 0;
    for (int i = 0; i < MAX; i++) {
        if (adj[node][i] && !visited[i]) {
            int dist = dfs(i) + 1;
            if (dist > max1) {
                max2 = max1;
                max1 = dist;
            } else if (dist > max2) {
                max2 = dist;
            }
        }
    }
    if (max1 + max2 > max_diameter) {
        max_diameter = max1 + max2;
    }
    return max1;
}

int main() {
    adj[0][1] = 1;
    adj[1][0] = 1;
    adj[1][2] = 1;
    // Missing adj[2][1] = 1;

    dfs(0);
    printf("%d\n", max_diameter);
    return 0;
}
AThe diameter will be underestimated due to missing edge
BThe diameter will be overestimated due to double counting
CCompilation error due to missing semicolon
DSegmentation fault due to invalid memory access
Attempts:
2 left
💡 Hint
Check if the tree edges are correctly bidirectional.
🧠 Conceptual
expert
1:30remaining
Why DP on trees is efficient for diameter calculation
Why is the DP approach on trees preferred over brute force for finding the diameter of a tree?
AIt avoids repeated calculations by storing intermediate results, reducing time complexity to O(n)
BIt uses more memory but runs slower than brute force
CIt only works for binary trees, not general trees
DIt requires sorting all nodes which increases complexity
Attempts:
2 left
💡 Hint
Think about how DP avoids repeated work in recursive tree traversal.