‹ 上一题 下一题 ›

g6-05255. GESP-6级-2026-09-单选-第09题 未做

以下代码实现二叉树左子树优先的深度优先搜索算法,则横线上应填写(  )。

void dfs(TreeNode *root) {
    if (root == nullptr)
        return;
    stack<TreeNode *> s;
    s.push(root);
    while (!s.empty()) {
        TreeNode *node = s.top();
        s.pop();
        cout << node->value << " ";
        ———————————————————————— // 在此处填入代码
    }
}
  • A. ```cpp
    s.push(node->right);
    s.push(node->left);
- **B.** ```cpp
s.push(node->left);
s.push(node->right);
  • C. ```cpp
    if (node->right)
    s.push(node->right);
    if (node->left)
    s.push(node->left);
- **D.** ```cpp
if (node->left)
    s.push(node->left);
if (node->right)
    s.push(node->right);
未选择
输入
输出

            
答案