Complete the code to return the left child of a node in a BST.
TreeNode leftChild = node.[1];The left child of a node in a BST is accessed using node.left.
Complete the code to find the minimum node in a BST subtree.
while (current.[1] != null) { current = current.[1]; } return current;
To find the minimum node, keep going left until no left child exists.
Fix the error in the code to find the inorder successor when the node has no right child.
TreeNode successor = null; TreeNode ancestor = root; while (ancestor != node) { if (node.val < ancestor.val) { successor = ancestor; ancestor = ancestor.[1]; } else { ancestor = ancestor.right; } } return successor;
When moving down the tree, if node's value is less than ancestor's, go left to find successor.
Fill both blanks to complete the method that finds the inorder successor in a BST.
if (node.[1] != null) { return findMin(node.[2]); }
If the node has a right child, the successor is the minimum node in the right subtree.
Fill all three blanks to complete the findMin method and the inorderSuccessor method logic.
private TreeNode findMin(TreeNode node) {
while (node.[1] != null) {
node = node.[2];
}
return node;
}
public TreeNode inorderSuccessor(TreeNode root, TreeNode node) {
if (node.[3] != null) {
return findMin(node.right);
}
// Additional logic omitted for brevity
return null;
}The findMin method goes left until no left child exists. The inorderSuccessor checks if the node has a right child.
