Complete the code to declare the structure for a binary search tree node.
typedef struct TreeNode {
int val;
struct TreeNode *[1];
struct TreeNode *right;
} TreeNode;The left pointer is used to point to the left child in a BST node.
Complete the code to perform in-order traversal of the BST.
void inorderTraversal(TreeNode* root) {
if (root == NULL) return;
inorderTraversal(root->[1]);
printf("%d ", root->val);
inorderTraversal(root->right);
}In-order traversal visits the left subtree first, so we use root->left.
Fix the error in the function that searches for a value in the BST.
int searchBST(TreeNode* root, int target) {
if (root == NULL) return 0;
if (root->val == target) return 1;
if (target < root->val) return searchBST(root->[1], target);
else return searchBST(root->right, target);
}When target is less than root value, search left subtree using root->left.
Fill both blanks to complete the function that checks if there exist two elements in BST that sum to target.
int findTarget(TreeNode* root, TreeNode* curr, int k) {
if (curr == NULL) return 0;
int complement = k - curr->val;
if (complement != curr->val && searchBST(root, [1])) return 1;
return findTarget(root, curr->[2], k) || findTarget(root, curr->right, k);
}The complement value is searched in the BST, and the traversal moves to the left child next.
Fill all three blanks to complete the main function that initiates the two sum check in BST.
int findTargetInBST(TreeNode* root, int k) {
if (root == NULL) return 0;
return findTarget(root, [1], [2]);
}
// Usage example:
// int result = findTargetInBST(root, [3]);The function starts with the root node and target k, and the example call uses k as the target sum.
