Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to visit the left subtree first in inorder traversal.
DSA C#
void InorderTraversal(Node node) {
if (node == null) return;
InorderTraversal(node[1]);
Console.Write(node.Value + " -> ");
InorderTraversal(node.Right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using node.Right instead of node.Left.
Trying to access node.Parent which is not part of traversal here.
✗ Incorrect
In inorder traversal, we visit the left child first, so we use node.Left.
2fill in blank
mediumComplete the code to print the root node's value during inorder traversal.
DSA C#
void InorderTraversal(Node node) {
if (node == null) return;
InorderTraversal(node.Left);
Console.Write(node[1] + " -> ");
InorderTraversal(node.Right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Printing node.Left or node.Right instead of node.Value.
Using node.Data which may not exist in this class.
✗ Incorrect
The root node's value is stored in node.Value, which we print in the middle of inorder traversal.
3fill in blank
hardFix the error in the inorder traversal call to visit the right subtree.
DSA C#
void InorderTraversal(Node node) {
if (node == null) return;
InorderTraversal(node.Left);
Console.Write(node.Value + " -> ");
InorderTraversal(node[1]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling InorderTraversal(node.Left) again instead of node.Right.
Using node.Parent which is not part of traversal here.
✗ Incorrect
In inorder traversal, after visiting the root, we visit the right subtree using node.Right.
4fill in blank
hardFill both blanks to complete the inorder traversal method with correct recursive calls.
DSA C#
void InorderTraversal(Node node) {
if (node == null) return;
InorderTraversal(node[1]);
Console.Write(node.Value + " -> ");
InorderTraversal(node[2]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right calls.
Using node.Parent or node.Next which are not part of traversal.
✗ Incorrect
Inorder traversal visits left subtree first, then root, then right subtree.
5fill in blank
hardFill all three blanks to create a dictionary that maps node values to their inorder traversal index.
DSA C#
Dictionary<int, int> inorderIndex = new Dictionary<int, int>(); int index = 0; void InorderTraversalIndex(Node node) { if (node == null) return; InorderTraversalIndex(node[1]); inorderIndex[node[2]] = index[3]; InorderTraversalIndex(node.Right); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.Right instead of node.Left for first call.
Using -- instead of ++ for index.
Using node.Data instead of node.Value.
✗ Incorrect
We visit left subtree first, then assign current node's value to index and increment index.
