Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the maximum value node in the left subtree.
DSA Rust
fn max_node(mut node: Option<&Box<TreeNode>>) -> Option<&Box<TreeNode>> {
while let Some(ref current) = node {
if current.right.is_none() {
return node;
}
node = current.[1].as_ref();
}
None
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' will find the minimum, not maximum.
Trying to access 'parent' which is not stored in this node structure.
✗ Incorrect
To find the maximum node in a 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 the root to find the inorder predecessor.
DSA Rust
fn inorder_predecessor(root: Option<&Box<TreeNode>>, key: i32) -> Option<&Box<TreeNode>> {
let mut current = root;
while let Some(node) = current {
if node.value == key {
if node.left.is_some() {
return max_node(node.[1].as_ref());
} else {
break;
}
} else if key < node.value {
current = node.left.as_ref();
} else {
current = node.right.as_ref();
}
}
None
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' will lead to wrong subtree.
Trying to access 'parent' which is not stored.
✗ Incorrect
The inorder predecessor is the maximum node in the left subtree, so we move to the left child first.
3fill in blank
hardFix the error in updating the predecessor when the node has no left child.
DSA Rust
fn inorder_predecessor(root: Option<&Box<TreeNode>>, key: i32) -> Option<&Box<TreeNode>> {
let mut predecessor = None;
let mut current = root;
while let Some(node) = current {
if key > node.value {
predecessor = current;
current = node.[1].as_ref();
} else {
current = node.left.as_ref();
}
}
predecessor
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' will cause infinite loop or wrong traversal.
Trying to access 'parent' which is not stored.
✗ Incorrect
When key is greater than node value, move to the right child to find a closer predecessor candidate.
4fill in blank
hardFill both blanks to complete the function that finds the inorder predecessor when the node has no left subtree.
DSA Rust
fn inorder_predecessor(root: Option<&Box<TreeNode>>, key: i32) -> Option<&Box<TreeNode>> {
let mut predecessor = None;
let mut current = root;
while let Some(node) = current {
if key > node.value {
predecessor = current;
current = node.[1].as_ref();
} else {
current = node.[2].as_ref();
}
}
predecessor
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right pointers causes wrong traversal.
Trying to access 'parent' which is not stored.
✗ Incorrect
When key is greater, move right to find a closer predecessor; else move left to continue search.
5fill in blank
hardFill all three blanks to complete the full inorder predecessor function combining both cases.
DSA Rust
fn inorder_predecessor(root: Option<&Box<TreeNode>>, key: i32) -> Option<&Box<TreeNode>> {
let mut current = root;
while let Some(node) = current {
if node.value == key {
if node.left.is_some() {
return max_node(node.[1].as_ref());
} else {
break;
}
} else if key < node.value {
current = node.[2].as_ref();
} else {
current = node.[3].as_ref();
}
}
let mut predecessor = None;
current = root;
while let Some(node) = current {
if key > node.value {
predecessor = current;
current = node.right.as_ref();
} else {
current = node.left.as_ref();
}
}
predecessor
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' which is not stored.
Swapping left and right pointers.
✗ Incorrect
The predecessor is max node in left subtree if left exists; else search path updating predecessor when moving right.
