Complete the code to declare a tree node struct with an integer value.
typedef struct TreeNode {
int [1];
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;The field name for the integer value in the tree node struct is commonly value.
Complete the code to check if a graph has a cycle using DFS.
bool hasCycle(int node, bool visited[], bool recStack[]) {
visited[node] = true;
recStack[node] = true;
for (int i = 0; i < graph[node].neighborCount; i++) {
int neighbor = graph[node].neighbors[i];
if (!visited[neighbor] && hasCycle(neighbor, visited, recStack))
return [1];
else if (recStack[neighbor])
return true;
}
recStack[node] = false;
return false;
}When a cycle is detected in DFS, the function should return true.
Fix the error in the code that checks if a tree is a valid binary search tree (BST).
bool isValidBST(struct TreeNode* root, int min, int max) {
if (root == NULL) return true;
if (root->value [1]= min || root->value [2]= max) return false;
return isValidBST(root->left, min, root->value) && isValidBST(root->right, root->value, max);
}The BST property requires node values to be strictly between min and max, so using <= or >= is correct to reject equal values.
Fill both blanks to create a graph adjacency list representation using arrays.
typedef struct {
int [1];
int [2][MAX_NEIGHBORS];
} GraphNode;The struct stores the count of neighbors in neighborCount and the list of neighbors in neighbors.
Fill all three blanks to implement a function that adds an edge to an undirected graph adjacency list.
void addEdge(GraphNode graph[], int u, int v) {
graph[u].[1][graph[u].[2]] = v;
graph[u].[2]++;
graph[v].[1][graph[v].[2]] = u;
graph[v].[2]++;
}The function adds v to u's neighbors and increments neighborCount, then does the same for v adding u.