Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the root node's value first in preorder traversal.
DSA C#
Console.WriteLine(node[1]);Attempts:
3 left
💡 Hint
Common Mistakes
Trying to print node.Left or node.Right instead of node.Value.
Using node.Parent which is not part of preorder traversal.
✗ Incorrect
In preorder traversal, we first visit the root node's value, which is accessed by the .Value property.
2fill in blank
mediumComplete the code to recursively traverse the left subtree in preorder.
DSA C#
if (node.Left != null) [1](node.Left);
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the wrong traversal function like TraverseInorder or TraversePostorder.
Not calling the function recursively.
✗ Incorrect
In preorder traversal, we recursively call the same preorder function on the left child.
3fill in blank
hardFix the error in the preorder traversal to correctly visit the right subtree after the left.
DSA C#
if (node.Right != null) [1](node.Right);
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a different traversal function on the right subtree.
Forgetting to traverse the right subtree.
✗ Incorrect
After visiting the root and left subtree, preorder traversal visits the right subtree by calling TraversePreorder on node.Right.
4fill in blank
hardFill both blanks to complete the preorder traversal method visiting root, left, then right.
DSA C#
public void TraversePreorder(Node node) {
if (node == null) return;
Console.WriteLine(node[1]);
if (node.Left != null) [2](node.Left);
if (node.Right != null) TraversePreorder(node.Right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property like .Left instead of .Value for printing.
Calling the wrong traversal function on left child.
✗ Incorrect
We print the node's value first, then recursively call TraversePreorder on the left child.
5fill in blank
hardFill all three blanks to implement preorder traversal printing root, then left subtree, then right subtree.
DSA C#
public void TraversePreorder(Node node) {
if (node == null) return;
Console.WriteLine(node[1]);
if (node.Left != null) [2](node.Left);
if (node.Right != null) [3](node.Right);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using TraverseInorder or PrintNode instead of TraversePreorder for recursion.
Printing node.Left or node.Right instead of node.Value.
✗ Incorrect
We print node.Value, then recursively call TraversePreorder on left and right children.
