Question 1:
1. You are given a binary tree, and your task is to convert it
into a doubly linked list in-place (i.e., reusing the tree
nodes as doubly linked list nodes) such that the nodes
appear in in-order traversal order. Further, you must use
a stack to perform this conversion iteratively (no recursion
allowed). The left pointer of each node should point to the
previous node in the doubly linked list, and the right
pointer should point to the next node.
Input Binary Tree:
4
/\
2 5
/\
1 3
Output Doubly Linked List:
1 <-> 2 <-> 3 <-> 4 <-> 5
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left, *right;
} Node;
typedef struct Queue {
Node* node;
struct Queue* next;
} Queue;
Node* newNode(int data) {
if (data == -1) return NULL;
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = node->right = NULL;
return node;
void enqueue(Queue** front, Queue** rear, Node* node) {
if (!node) return;
Queue* qNode =(Queue*)malloc(sizeof(Queue));
qNode->node = node;
qNode->next= NULL;
if (*rear) (*rear)->next = qNode;
*rear = qNode;
if (*front == NULL) *front = *rear;
Node* dequeue(Queue** front, Queue** rear) {
if (!*front) return NULL;
Queue* temp =*front;
*front = (*front)->next;
if (!*front) *rear = NULL;
Node* node = temp->node;
free(temp);
return node;
}
Node* buildTree() {
int data;
printf("Enter root value (-1 for NULL): ");
scanf("%d", &data);
Node* root = newNode(data);
if (!root) return NULL;
Queue *front = NULL, *rear = NULL;
enqueue(&front, &rear, root);
while (front) {
Node* curr = dequeue(&front, &rear);
printf("Enter left child of %d (-1 for NULL): ", curr->data);
scanf("%d", &data);
curr->left = newNode(data);
enqueue(&front, &rear, curr->left);
printf("Enter right child of %d (-1 for NULL): ", curr->data);
scanf("%d", &data);
curr->right = newNode(data);
enqueue(&front, &rear, curr->right);
return root;
typedef struct Stack {
Node* node;
struct Stack* next;
} Stack;
void push(Stack** top, Node* node) {
Stack* sNode = (Stack*)malloc(sizeof(Stack));
sNode->node = node;
sNode->next = *top;
*top = sNode;
Node* pop(Stack** top) {
if (!*top) return NULL;
Stack* temp = *top;
*top = (*top)->next;
Node* node = temp->node;
free(temp);
return node;
int isEmpty(Stack* top) {
return top == NULL;
Node* treeToDLL(Node* root) {
if (!root) return NULL;
Stack* stack = NULL;
Node *curr = root, *head = NULL, *prev = NULL;
while (!isEmpty(stack) || curr) {
while (curr) {
push(&stack, curr);
curr = curr->left;
curr = pop(&stack);
if (!prev) head = curr;
else {
prev->right = curr;
curr->left = prev;
prev = curr;
curr = curr->right;
return head;
void printDLL(Node* head) {
while (head) {
printf("%d", head->data);
if (head->right) printf(" <-> ");
head = head->right;
printf("\n");
int main() {
Node* root = buildTree();
Node* head = treeToDLL(root);
printf("Doubly Linked List:\n");
printDLL(head);
return 0;
Question 2:
2. You are given a weighted, undirected graph
G=(V,E)G=(V,E) with positive edge weights. Your task is to:
• Compute the Minimum Spanning Tree (MST) of the
graph using any algorithm (e.g., Kruskal’s or Prim’s).
• Compute the Shortest Path Tree (SPT) rooted at a
given source vertex ss using Dijkstra’s algorithm.
• Analyze the conditions under which the MST and SPT
could be identical for the same graph and source
vertex.
A --1--> B
| / |
4 2 |
| / 3
C --5--> D
Code:
#include <stdio.h>
#include <stdlib.h>
#define INF 99999
#define N 4
typedef struct {
int u, v, w;
} Edge;
typedef struct {
int p, r;
} Subset;
int find(Subset s[], int i) {
if (s[i].p != i)
s[i].p = find(s, s[i].p);
return s[i].p;
}
void unite(Subset s[], int x, int y) {
int rx = find(s, x), ry = find(s, y);
if (s[rx].r < s[ry].r)
s[rx].p = ry;
else if (s[rx].r > s[ry].r)
s[ry].p = rx;
else {
s[ry].p = rx;
s[rx].r++;
int cmp(const void *a, const void *b) {
return ((Edge *)a)->w - ((Edge *)b)->w;
void mst(Edge e[], int m) {
Subset s[N];
Edge res[N - 1];
int i = 0, j = 0;
for (int v = 0; v < N; v++) {
s[v].p = v;
s[v].r = 0;
qsort(e, m, sizeof(Edge), cmp);
while (j < N - 1 && i < m) {
Edge next = e[i++];
int x = find(s, next.u), y = find(s, next.v);
if (x != y) {
res[j++] = next;
unite(s, x, y);
printf("\nMST:\n");
int cost = 0;
for (i = 0; i < j; i++) {
printf("%c -- %d --> %c\n", 'A' + res[i].u, res[i].w, 'A' + res[i].v);
cost += res[i].w;
printf("Total Cost: %d\n", cost);
void spt(int g[N][N], int src) {
int d[N], vis[N], p[N];
for (int i = 0; i < N; i++) {
d[i] = INF;
vis[i] = 0;
p[i] = -1;
d[src] = 0;
for (int cnt = 0; cnt < N - 1; cnt++) {
int min = INF, u;
for (int v = 0; v < N; v++)
if (!vis[v] && d[v] < min)
min = d[v], u = v;
vis[u] = 1;
for (int v = 0; v < N; v++)
if (!vis[v] && g[u][v] && d[u] + g[u][v] < d[v]) {
d[v] = d[u] + g[u][v];
p[v] = u;
printf("\nSPT from A:\n");
for (int i = 0; i < N; i++) {
if (i != src) {
printf("A --> %c | Cost: %d | Path: ", 'A' + i, d[i]);
int j = i;
while (p[j] != -1) {
printf("%c <- ", 'A' + j);
j = p[j];
printf("A\n");
int main() {
Edge e[] = {
{0, 1, 1}, {0, 2, 4}, {1, 2, 2}, {1, 3, 3}, {2, 3, 5}
};
int m = sizeof(e) / sizeof(e[0]);
int g[N][N] = {
{0, 1, 4, 0},
{1, 0, 2, 3},
{4, 2, 0, 5},
{0, 3, 5, 0}
};
mst(e, m);
spt(g, 0);
return 0;