Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a binary tree node struct.
DSA C
struct Node {
int data;
struct Node* [1];
struct Node* right;
};Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' or 'prev' which are used in linked lists.
Using 'child' which is too generic.
✗ Incorrect
The left child pointer in a binary tree node is usually named 'left'.
2fill in blank
mediumComplete the code to check if a node is a leaf node.
DSA C
int isLeaf(struct Node* node) {
return (node->left == NULL && node->[1] == NULL);
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'next' or 'parent' pointers which do not exist in binary trees.
Checking only one child pointer.
✗ Incorrect
A leaf node has both left and right pointers as NULL.
3fill in blank
hardFix the error in the function to print the left boundary excluding leaf nodes.
DSA C
void printLeftBoundary(struct Node* node) {
if (node) {
if (node->left) {
printf("%d ", node->data);
printLeftBoundary(node->[1]);
} else if (node->right) {
printf("%d ", node->data);
printLeftBoundary(node->right);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using right child pointer first which prints the wrong boundary.
Using non-existent pointers like 'next' or 'parent'.
✗ Incorrect
To traverse the left boundary, we move down the left child first.
4fill in blank
hardFill both blanks to print the right boundary excluding leaf nodes in bottom-up order.
DSA C
void printRightBoundary(struct Node* node) {
if (node) {
if (node->[1]) {
printRightBoundary(node->[1]);
printf("%d ", node->data);
} else if (node->[2]) {
printRightBoundary(node->[2]);
printf("%d ", node->data);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Printing nodes before recursive call causing top-down order.
Using wrong child pointers.
✗ Incorrect
For right boundary, traverse down the right child first, then left child if right is NULL.
5fill in blank
hardFill all three blanks to complete the boundary traversal function.
DSA C
void printBoundary(struct Node* root) {
if (root) {
printf("%d ", root->data);
printLeftBoundary(root->[1]);
printLeaves(root->[2]);
printLeaves(root->[3]);
printRightBoundary(root->right);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' pointer which does not exist.
Mixing left and right pointers in wrong order.
✗ Incorrect
Left boundary starts from root->left, leaves from both left and right subtrees, right boundary from root->right.
