Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the data member for storing the node's value.
DSA C++
struct Node {
int [1];
Node* left;
Node* right;
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node' which is confusing as it suggests the whole node, not just the value.
Using overly long or unclear names like 'dataValue' or 'valNode'.
✗ Incorrect
The data member to store the node's value is commonly named 'value'.
2fill in blank
mediumComplete the code to initialize the left child pointer to nullptr in the constructor.
DSA C++
struct Node {
int value;
Node* left;
Node* right;
Node(int val) : value(val), [1](nullptr), right(nullptr) {}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing the wrong pointer like 'right' instead of 'left'.
Trying to initialize 'value' again which is already initialized.
✗ Incorrect
The left child pointer is initialized to nullptr using 'left(nullptr)'.
3fill in blank
hardFix the error in the constructor initializer list to correctly initialize the right child pointer.
DSA C++
struct Node {
int value;
Node* left;
Node* right;
Node(int val) : value(val), left(nullptr), [1](nullptr) {}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing 'left' twice instead of 'right'.
Trying to initialize 'value' again which is already done.
✗ Incorrect
The right child pointer should be initialized as 'right(nullptr)' in the initializer list.
4fill in blank
hardFill both blanks to complete the function that checks if a node is a leaf (no children).
DSA C++
bool isLeaf(Node* node) {
return node->[1] == nullptr && node->[2] == nullptr;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'value' or 'node' instead of child pointers.
Checking only one child pointer instead of both.
✗ Incorrect
A leaf node has both left and right pointers set to nullptr.
5fill in blank
hardFill all three blanks to complete the function that creates a new node with given value and returns its pointer.
DSA C++
Node* createNode(int val) {
Node* newNode = new Node([1]);
newNode->[2] = nullptr;
newNode->[3] = nullptr;
return newNode;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'value' instead of 'val' to constructor.
Setting wrong members like 'value' instead of child pointers.
Forgetting to initialize one of the child pointers.
✗ Incorrect
The constructor is called with 'val', and both child pointers 'left' and 'right' are set to nullptr.