Why I Am Coding In C 😭
Code
# Code
```c []
typedef struct {
    int *arr;
    int len;
    int filled;
} Vector;

typedef struct {
    Vector **vec;
    int len;
    int filled;
} FatherVector;

void InitVector(Vector *v, int initial_size) {
    v->arr = (int *)malloc(initial_size * sizeof(int));
    if (!v->arr) exit(EXIT_FAILURE);
    v->len = initial_size;
    v->filled = 0;
}

void PushBack(Vector *v, int num) {
    if (v->filled >= v->len) {
        int new_len = 2 * v->len;
        int *new_arr = (int *)realloc(v->arr, new_len * sizeof(int));
        if (!new_arr) exit(EXIT_FAILURE);
        v->arr = new_arr;
        v->len = new_len;
    }
    v->arr[v->filled] = num;
    v->filled++;
}

void FreeVector(Vector *v) {
    free(v->arr);
    v->arr = NULL;
    v->len = 0;
    v->filled = 0;
}

void InitFatherVector(FatherVector *fv, int initial_size) {
    fv->vec = (Vector **)malloc(initial_size * sizeof(Vector *));
    if (!fv->vec) exit(EXIT_FAILURE);
    fv->len = initial_size;
    fv->filled = 0;
}

void PushBackFV(FatherVector *fv, Vector *v) {
    if (fv->filled >= fv->len) {
        int new_len = 2 * fv->len;
        Vector **new_vec = (Vector **)realloc(fv->vec, new_len * sizeof(Vector *));
        if (!new_vec) exit(EXIT_FAILURE);
        fv->vec = new_vec;
        fv->len = new_len;
    }
    fv->vec[fv->filled] = v;
    fv->filled++;
}

void FreeFatherVector(FatherVector *fv) {
    for (int i = 0; i < fv->filled; i++) {
        FreeVector(fv->vec[i]);
        free(fv->vec[i]);
    }
    free(fv->vec);
    fv->vec = NULL;
    fv->len = 0;
    fv->filled = 0;
}

int lendfs(struct TreeNode* root) {
    if (!root) return 0;
    return lendfs(root->left) + lendfs(root->right) + 1;
}

FatherVector* bfs(struct TreeNode* root, int n) {
    if (!root) return NULL;
    FatherVector *fv = (FatherVector *)malloc(sizeof(FatherVector));
    InitFatherVector(fv, n / 2);
    struct TreeNode **queue = (struct TreeNode **)malloc(n * sizeof(struct TreeNode *));
    int front = 0, back = 0;
    queue[back++] = root;
    while (front < back) {
        int level_size = back - front;
        Vector *level = (Vector *)malloc(sizeof(Vector));
        InitVector(level, level_size);
        for (int i = 0; i < level_size; i++) {
            struct TreeNode *current = queue[front++];
            PushBack(level, current->val);
            if (current->left) queue[back++] = current->left;
            if (current->right) queue[back++] = current->right;
        }
        PushBackFV(fv, level);
    }
    free(queue);
    return fv;
}

typedef struct { int value, index; } Pair;

int pairCompare(const void *a, const void *b) {
    return ((Pair *)a)->value - ((Pair *)b)->value;
}

int countSwapsToSort(Vector *level) {
    int *arr = level->arr;
    int n = level->filled;
    Pair *pairs = (Pair *)malloc(n * sizeof(Pair));
    for (int i = 0; i < n; i++) {
        pairs[i].value = arr[i];
        pairs[i].index = i;
    }
    qsort(pairs, n, sizeof(Pair), pairCompare);
    int *visited = (int *)calloc(n, sizeof(int));
    int swaps = 0;
    for (int i = 0; i < n; i++) {
        if (visited[i] || pairs[i].index == i) continue;
        int cycle_size = 0, j = i;
        while (!visited[j]) {
            visited[j] = 1;
            j = pairs[j].index;
            cycle_size++;
        }
        if (cycle_size > 1) swaps += cycle_size - 1;
    }
    free(pairs);
    free(visited);
    return swaps;
}

int minimumOperations(struct TreeNode* root) {
    if (!root) return 0;
    int n = lendfs(root);
    if(n==1) return 0;
    FatherVector *fv = bfs(root, n);
    int total_swaps = 0;
    for (int i = 0; i < fv->filled; i++) {
        total_swaps += countSwapsToSort(fv->vec[i]);
    }
    FreeFatherVector(fv);
    free(fv);
    return total_swaps;
}
```
['C']