以下代码使用队列查找二叉树中的目标节点,请在横线处填入代码。
TreeNode* findNode(TreeNode* root, int target) {
if (root == nullptr) return nullptr;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* current = q.front();
q.pop();
if (current->val == target) {
return current; // 找到目标节点
}
____________ // 在此处填入代码
}
return nullptr; // 未找到目标节点
}
- A.
if (current->left) q.push(current->left);
if (current->right) q.push(current->right);
- B.
if (current->left) q.pop(current->left);
if (current->right) q.pop(current->right);
- C.
if (current->left) q.front(current->left);
if (current->right) q.front(current->right);
- D.
if (current->left) q.push(current->right);
if (current->right) q.push(current->left);