首页 / 客观题库

Q80570 - 树43

基础数据结构

题目( 单选题 )

给定一棵二叉树,采用广度优先搜索 (BFS) 算法,返回右视图所有节点的值。其中右视图定义为:二叉树的右视图是从树的右侧看过去时可见的节点集合,即右视图中的每个节点都是某一层中最右侧的节点。

struct TreeNode {

    int val;

    TreeNode* left;

    TreeNode* right;

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

};

vector<int> rightSideView(TreeNode* root) {

    unordered_map<int, int> rightmostValueAtDepth;

    int max_depth = -1;

    queue<TreeNode*> nodeQueue;

    queue<int> depthQueue;

    nodeQueue.push(root);

    depthQueue.push(0);

    while (!nodeQueue.empty()) {

       TreeNode* node = nodeQueue.front(); nodeQueue.pop();

       int depth = depthQueue.front(); depthQueue.pop();

       if (node != NULL) {

           max_depth = max(max_depth, depth);

           rightmostValueAtDepth[depth] = node->val;

           nodeQueue.push(node->left);

           nodeQueue.push(node->right);

           depthQueue.push(________);

           depthQueue.push(________);

       }

    }

    vector<int> rightView;

    for (int depth = 0; ________; ++depth) {

        rightView.push_back(rightmostValueAtDepth[depth]);

    }

    return rightView;

};

A

depth

depth

depth < max_depth

B

depth + 1

depth + 1

depth <= max_depth

C

depth + 1

depth + 1

depth < max_depth

D

depth

depth

depth <= max_depth

意见反馈

    最多上传3张图片,格式为JPG、PNG、JPEG,单张不超过5MB

    注册

    发送验证码

    密码必须包含数字、字母和特殊字符

    找回密码

    发送验证码

    密码必须包含数字、字母和特殊字符

    运行 ID:67149

    • 测试点1:Accepted
    • 用时:0 ms
    • 内存:288 kb
    • 测试点2:Accepted
    • 用时:0 ms
    • 内存:288 kb
    输入
    203
    输出
    203

    test

    测评信息

    错误.in文件下载

    错误.out文件下载

    运行 ID:67149

    2019-01-24 15:06:36