Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the maximum value node in a BST subtree.
DSA Python
def find_max(node): current = node while current.[1] is not None: current = current.right return current
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' will find the minimum, not maximum.
Trying to access 'parent' attribute which may not exist.
✗ Incorrect
To find the maximum value node in a BST subtree, we keep moving to the right child until there is no right child.
2fill in blank
mediumComplete the code to move to the left child of a node.
DSA Python
def move_left(node): if node is not None: return node.[1] return None
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' will move to the wrong child.
Accessing 'value' instead of child nodes.
✗ Incorrect
To move to the left child of a node, access the 'left' attribute.
3fill in blank
hardFix the error in the code to correctly find the inorder predecessor when the left subtree exists.
DSA Python
def inorder_predecessor(root, node): if node.left is not None: return find_max(node.[1]) return None
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right will find the wrong subtree.
Using node.parent without checking if it exists.
✗ Incorrect
The inorder predecessor is the maximum node in the left subtree, so we call find_max on node.left.
4fill in blank
hardFill both blanks to move up the tree to find the inorder predecessor when no left subtree exists.
DSA Python
def inorder_predecessor(root, node): if node.left is None: parent = node.[1] while parent is not None and node == parent.[2]: node = parent parent = parent.parent return parent return find_max(node.left)
Attempts:
3 left
💡 Hint
Common Mistakes
Checking node == parent.left instead of parent.right.
Not updating node and parent inside the loop.
✗ Incorrect
When no left subtree exists, move up using parent pointers until the node is not the right child. So, parent is node.parent and we check node == parent.right.
5fill in blank
hardFill all three blanks to complete the full inorder predecessor function.
DSA Python
def inorder_predecessor(root, node): if node.[1] is not None: return find_max(node.[2]) parent = node.[3] while parent is not None and node == parent.right: node = parent parent = parent.parent return parent
Attempts:
3 left
💡 Hint
Common Mistakes
Using right instead of left for subtree check.
Not moving up correctly using parent pointers.
✗ Incorrect
If node has a left child, predecessor is max in left subtree (node.left). Otherwise, move up using parent pointers.
