Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the left child of a BST node.
DSA Go
func getLeftChild(node *Node) *Node {
return node.[1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Right' instead of 'Left' to get the left child.
Trying to access 'Value' instead of child nodes.
✗ Incorrect
The left child of a BST node is accessed using the 'Left' field.
2fill in blank
mediumComplete the code to find the maximum node in a BST subtree.
DSA Go
func findMax(node *Node) *Node {
current := node
for current.[1] != nil {
current = current.[1]
}
return current
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Going left instead of right to find maximum.
Checking 'Value' instead of child pointers.
✗ Incorrect
The maximum node in a BST subtree is found by going right until no more right child exists.
3fill in blank
hardFix the error in the code to find the inorder predecessor when left subtree exists.
DSA Go
func inorderPredecessor(node *Node) *Node {
if node.Left != nil {
pred := node.Left
for pred.[1] != nil {
pred = pred.[1]
}
return pred
}
return nil
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Going left instead of right inside left subtree.
Returning the left child directly without traversal.
✗ Incorrect
To find the inorder predecessor in the left subtree, go to the rightmost node of the left child.
4fill in blank
hardFill both blanks to find the inorder predecessor when no left subtree exists.
DSA Go
func inorderPredecessor(node *Node) *Node {
if node.Left == nil {
pred := node.[1]
for pred != nil && node == pred.[2] {
node = pred
pred = pred.[1]
}
return pred
}
return nil
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Right instead of Left to check child relation.
Not moving up the tree using Parent pointer.
✗ Incorrect
When no left subtree, move up using Parent pointer until node is not a left child.
5fill in blank
hardFill all three blanks to complete the full inorder predecessor function.
DSA Go
func inorderPredecessor(node *Node) *Node {
if node.Left != nil {
pred := node.Left
for pred.[1] != nil {
pred = pred.[1]
}
return pred
}
pred := node.[2]
for pred != nil && node == pred.[3] {
node = pred
pred = pred.[2]
}
return pred
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing Left and Right pointers incorrectly.
Not handling both cases of predecessor properly.
✗ Incorrect
If left subtree exists, go rightmost in left subtree. Else, move up parents until node is not left child.