测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A23794. 请将下列树的深度优先遍历代码补充完整,横线处应填入( )。struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; void dfs(TreeNode* root) { if (!root) return; __…

单选题 困难

题目描述

请将下列树的深度优先遍历代码补充完整,横线处应填入(    )。

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
void dfs(TreeNode* root) {
    if (!root) return;
    ______<TreeNode*> temp;  // 在此处填写代码
    temp.push(root);
    while (!temp.empty()) {
        TreeNode* node = temp.top();
        temp.pop();
        cout << node->val << " ";
        if (node->right) temp.push(node->right);
        if (node->left) temp.push(node->left);
    }
}

选项(单选)

上一题 下一题