Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add the root node's data to the boundary list.
DSA C#
boundary.Add([1].data);Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name other than 'root' which is not defined.
Trying to add the whole node instead of its data.
✗ Incorrect
The root node is the starting point of the boundary traversal, so we add root.data to the list.
2fill in blank
mediumComplete the code to check if a node is a leaf node.
DSA C#
bool isLeaf = (node.left == null && node.[1] == null);Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'child' or 'next' which are not valid properties.
Checking only one child instead of both.
✗ Incorrect
A leaf node has no left or right child, so both must be null.
3fill in blank
hardFix the error in the code to traverse the left boundary excluding leaf nodes.
DSA C#
while (curr != null && !isLeaf(curr)) { boundary.Add(curr.data); curr = curr.[1] != null ? curr.left : curr.right; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using curr.right to move left boundary which is incorrect.
Using invalid property names like 'parent' or 'child'.
✗ Incorrect
To traverse the left boundary, move to the left child if it exists; otherwise, move to the right child.
4fill in blank
hardFill both blanks to traverse the right boundary excluding leaf nodes in reverse order.
DSA C#
Stack<int> stack = new Stack<int>(); while (curr != null && !isLeaf(curr)) { stack.Push(curr.data); curr = curr.[1] != null ? curr.[1] : curr.[2]; } while (stack.Count > 0) { boundary.Add(stack.Pop()); }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right child in traversal.
Not using a stack to reverse the right boundary nodes.
✗ Incorrect
For right boundary traversal, move to right child if it exists; otherwise, move to left child.
5fill in blank
hardFill all three blanks to add all leaf nodes from left to right.
DSA C#
void addLeaves(Node node, List<int> boundary) {
if (node == null) return;
if (isLeaf(node)) {
boundary.Add(node.[1]);
return;
}
addLeaves(node.[2], boundary);
addLeaves(node.[3], boundary);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'data' for node's value.
Swapping left and right child traversal order.
✗ Incorrect
Add the node's data if it's a leaf, then recursively add leaves from left and right children.
