Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the left child of a node in a BST.
DSA C#
TreeNode leftChild = node.[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Right' instead of 'Left' to access the left child.
Trying to access a non-existent 'Parent' property.
✗ Incorrect
The left child of a node in a BST is accessed using the 'Left' property.
2fill in blank
mediumComplete the code to find the maximum node in a BST subtree.
DSA C#
while (current.[1] != null) { current = current.[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Going left instead of right to find the maximum.
Using 'Parent' which is not always available.
✗ Incorrect
The maximum node in a BST subtree is found by going as far right as possible.
3fill in blank
hardFix the error in the code to find the inorder predecessor when the left subtree is null.
DSA C#
while (node.[1] != null && node.[1].Value > key) { node = node.[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to move left or right when left subtree is null.
Not handling the case when parent is null.
✗ Incorrect
When left subtree is null, we move up to the parent to find the inorder predecessor.
4fill in blank
hardFill both blanks to complete the method that finds the inorder predecessor in a BST.
DSA C#
if (node.[1] != null) { TreeNode temp = node.[1]; while (temp.[2] != null) { temp = temp.[2]; } return temp; }
Attempts:
3 left
💡 Hint
Common Mistakes
Going left twice instead of left then right.
Using parent pointer incorrectly here.
✗ Incorrect
If left subtree exists, predecessor is the rightmost node in left subtree.
5fill in blank
hardFill all three blanks to complete the method that finds the inorder predecessor when left subtree is null.
DSA C#
TreeNode predecessor = null; while (node != null) { if (key > node.Value) { predecessor = node; node = node.[1]; } else { node = node.[2]; } } return predecessor; // predecessor is [3]
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right moves.
Returning the first node greater than key instead of smaller.
✗ Incorrect
When left subtree is null, move right if key is greater, else left; predecessor is last smaller node.
