Binary search trees - removing odd nodes
12:09 25 Apr 2023

I'm doing an exercise on Binary Search Trees, where I need to be removing odd nodes and at the end printing the key value of each node in a single line considering the pre-order path. I couldn't identify what could be wrong Here is my code


struct Node* remove_odd_nodes(struct Node* root) {
    if (root == NULL) {
        return NULL;
    }
    root->left = remove_odd_nodes(root->left);
    root->right = remove_odd_nodes(root->right);
    if (root->value % 2 == 1) {
        if (root->left == NULL && root->right == NULL) {
            free(root);
            return NULL;
        }
        if (root->left == NULL) {
            struct Node* temp = root->right;
            free(root);
            return temp;
        }
        if (root->right == NULL) {
            struct Node* temp = root->left;
            free(root);
            return temp;
        }
        struct Node* min_node = root->right;
        while (min_node->left != NULL) {
            min_node = min_node->left;
        }
        root->value = min_node->value;
        root->right = remove_odd_nodes(root->right);
    }
    return root;
}


I couldn't identify what could be wrong

c algorithm binary-tree binary-search-tree