Complete the code to start the backtracking recursion from the first position.
void solve() {
int start = [1];
backtrack(start);
}The backtracking usually starts from index 0 to cover all possibilities from the beginning.
Complete the code to check if the current choice is valid before proceeding.
if (isValid([1])) { backtrack(pos + 1); }
We check if the current position choice is valid before moving forward.
Fix the error in the backtracking base case to stop recursion correctly.
if (pos == [1]) { printSolution(); return; }
The base case is when the position reaches n, meaning all positions are processed.
Fill both blanks to correctly undo the choice after backtracking.
choices[pos] = [1]; backtrack(pos + 1); choices[pos] = [2];
We mark the choice as made (true), recurse, then undo it (false) to explore other options.
Fill all three blanks to build a decision tree node and recurse correctly.
struct Node {
int choice;
struct Node* [1];
struct Node* [2];
};
void explore(struct Node* node) {
if (node == NULL) return;
process(node->choice);
explore(node->[3]);
}The decision tree node has left and right children. We recurse on the left child in explore.