Subtask 1 (50 points) You will be given the level order traversal of the family tree as…
- User
Subtask 1 (50 points) You will be given the level order traversal of the family tree as an array level order of size n. You have to construct this family tree from scratch, assuming it is a binary tree. Note that the level order traversal will comprise Is and Os, where 1 denotes a valid node and 0 denotes a null node. Having created the tree of family members, you have to find the minimum number of phones m that needs to be distributed among them, given that each member who is given a phone can share their phone with their parent or their children. However, if someone is not given a phone, they can only be shared with ie they cannot share the phone further with others. 1/O Format Input Format n level_order [0] level_order[1] … level_order [n-1] The first line contains n, the length of array level_order. The second line contains level_order], which is the level-order traversal of the family tree. Output Format m The output will just be m, the minimum number of phones required.
I NEED A SOLUTION IN C LANGUAGE
- Assistant
- User
I NEED ACTUAL CODE NOT PAUEDOCODE
- Assistant
The code provided above is actual C code, not pseudocode. However, it lacks the tree memory cleanup function. Below I've added a
deleteTreefunction that releases the memory allocated for the tree. Make sure to call this function before your program ends to avoid memory leaks. - User
- Assistant
- User
ERROR! gcc /tmp/HTnGwE2FtS.c -lm /tmp/HTnGwE2FtS.c: In function 'buildTree': /tmp/HTnGwE2FtS.c:20:35: error: array subscript is not an integer 20 | if (*index >= n || level_order[index] == 0) { | ^ /tmp/HTnGwE2FtS.c:25:17: error: invalid initializer 25 | Node root = createNode(); | ^~~~~~~~~~ /tmp/HTnGwE2FtS.c:28:9: error: invalid type argument of '->' (have 'Node') 28 | root->left = buildTree(level_order, index, n); | ^~ /tmp/HTnGwE2FtS.c:29:9: error: invalid type argument of '->' (have 'Node') 29 | root->right = buildTree(level_order, index, n); | ^~ /tmp/HTnGwE2FtS.c:31:12: error: incompatible types when returning type 'Node' but 'Node *' was expected 31 | return root; | ^~~~ /tmp/HTnGwE2FtS.c: In function 'minPhones': /tmp/HTnGwE2FtS.c:36:14: error: invalid operands to binary == (have 'Node' and 'void *') 36 | if (node == NULL) {
- Assistant
Apologies once again for the oversight. It appears there have been additional errors in the code provided. Let's fix those and provide a corrected version:
#include <stdio.h> #include <stdlib.h> // Define a node in the binary tree typedef struct Node { struct Node* left; struct Node* right; } Node; // Function to create a new binary tree node Node* createNode() { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->left = newNode->right = NULL; return newNode; } // Function to construct the binary tree from its level order traversal Node* buildTree(int *level_order, int *index, int n) { if (*index >= n || level_order[*index] == 0) { (*index)++; return NULL; } Node* root = createNode(); (*index)++; root->left = buildTree(level_order, index, n); root->right = buildTree(level_order, index, n); return root; } // Function to calculate the minimum number of phones required int minPhones(Node* node) { if (node == NULL) { return 0; } int leftPhones = minPhones(node->left); int rightPhones = minPhones(node->right); int phonesNeeded = 1; // Node itself needs a phone if (node->left && leftPhones > 0) phonesNeeded--; // Can borrow from left child if (node->right && rightPhones > 0) phonesNeeded--; // Can borrow from right child return phonesNeeded + leftPhones + rightPhones; } // Function to delete the tree and free memory void deleteTree(Node* node) { if (node == NULL) { return; } deleteTree(node->left); deleteTree(node->right); free(node); } int main() { int n; scanf("%d", &n); int *level_order = (int*)malloc(n * sizeof(int)); for (int i = 0; i < n; i++) { scanf("%d", &level_order[i]); } int index = 0; Node *root = buildTree(level_order, &index, n); int m = minPhones(root); printf("%d\n", m); // Cleanup code deleteTree(root); free(level_order); return 0; }