Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a struct for a binary tree node.
DSA C
struct Node {
int data;
struct Node* [1];
struct Node* right;
};Attempts:
3 left
💡 Hint
Common Mistakes
Using 'child' or 'next' instead of 'left' for the left child pointer.
✗ Incorrect
The left child pointer in a binary tree node is usually named 'left'.
2fill in blank
mediumComplete the code to create a new node with given data.
DSA C
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->[1] = NULL;
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing the wrong pointer or forgetting to initialize the right pointer.
✗ Incorrect
The right child pointer must be initialized to NULL when creating a new node.
3fill in blank
hardFix the error in the code to assign the left child of root.
DSA C
root->[1] = createNode(5);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'child' or 'next' instead of 'left' pointer.
✗ Incorrect
The left child of the root node is assigned using the 'left' pointer.
4fill in blank
hardFill both blanks to assign left and right children to root.
DSA C
root->[1] = createNode(3); root->[2] = createNode(7);
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up left and right pointers or using incorrect pointer names.
✗ Incorrect
The left child is assigned to 'left' and the right child to 'right' pointers.
5fill in blank
hardFill all three blanks to print the root and its children data.
DSA C
printf("Root: %d\n", root->[1]); printf("Left Child: %d\n", root->[2]->data); printf("Right Child: %d\n", root->[3]->data);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'child' or missing 'data' when printing values.
✗ Incorrect
To print root data use 'data', for children use 'left' and 'right' pointers to access their data.
