在第 4.3.3 节中,我们掌握了 BFS 层序遍历。本实验考查层序遍历的经典变体——二叉树右视图。
题目
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值序列。
输入格式
- 一行以空格分隔的若干个 token,表示二叉树的层序遍历序列。
输出格式
- 一行以空格分隔的整数,表示右视图节点值序列。若为空树输出空行。
💡 输入处理与建树指引
- 读入序列: 直接使用
std::string token; while (std::cin >> token)循环读取输入放入std::vector<std::string> tokens中即可,C++ 会自动按空格和换行分词。 - 字符串转数字与 null 拦截:
- 遇到
"null"时,表示空子树,直接将子节点置为nullptr;切勿对"null"调用std::stoi("null")(会抛出std::invalid_argument异常导致崩溃); - 仅在
token != "null"时,才调用std::stoi(token)转为整数并创建有效节点new TreeNode(val)。
- 遇到
- 基于队列的 BFS 建树: 借助
std::queue<TreeNode*>存放父节点,队头出队后依次连接左右孩子,并将非空孩子入队。
样例
样例输入 1
input
1 2 3 null 5 null 4样例输出 1
output
1 3 4样例输入 2
input
1 2 3 4 null null null 5样例输出 2
output
1 3 4 5样例输入 3
input
1 2 null 3 null 4 null样例输出 3
output
1 2 3 4样例解释
text
样例 1 树形观测: 样例 2 树形观测(左侧更深):
1 <--- 看到 1 1 <--- 看到 1
/ \ / \
2 3 <--- 看到 3 2 3 <--- 看到 3
\ \ /
5 4 <--- 看到 4 4 <--- 看到 4
/
5 <--- 看到 5
右视图结果:1 3 4 右视图结果:1 3 4 5如何验证
先安装 Node.js、pnpm 和支持 C++17 的编译器。GNU Make 是首选入口,但不是强制依赖。
powershell
# 已进入本 Lab 目录
make doctor
make run
make run CASE=001-sample
make interactive
make scoreWindows 没有安装 Make 时,在仓库根目录使用完全相同的评分内核:
powershell
pnpm lab:doctor -- labs/chapter-04/exercise/E-04-05-binary-tree-right-side-view
pnpm lab:run -- labs/chapter-04/exercise/E-04-05-binary-tree-right-side-view
pnpm lab:run -- labs/chapter-04/exercise/E-04-05-binary-tree-right-side-view --case 001-sample
pnpm lab:score -- labs/chapter-04/exercise/E-04-05-binary-tree-right-side-viewmake run 在答案尚未全对时仍正常返回,避免 Make 把学习结果显示成工具故障;make score 是严格入口,只有 100 分才返回成功。标准输出参与判题,调试信息请写入标准错误。
题解
点击查看题解
核心思路
右视图的本质是每一层最右边的那个节点:
- 解法一(BFS 层序遍历):在每一层的
sz个节点中,只保留最后一个节点(即i == sz - 1时的节点值); - 解法二(DFS 根-右-左优先搜索):记录当前深度,当深度等于结果数组的大小时,说明是该深度首次被访问到的节点(即最右节点),直接加入结果。
复杂度分析
- 时间复杂度:
,每个节点访问一次。 - 空间复杂度:
(BFS 队列)或 (DFS 调用栈)。
点击查看参考代码
cpp
#include <iostream>
#include <vector>
#include <string>
#include <queue>
struct TreeNode {
int val = 0;
TreeNode* left = nullptr;
TreeNode* right = nullptr;
TreeNode() = default;
TreeNode(int x) : val(x) {}
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
TreeNode* buildTree(const std::vector<std::string>& tokens) {
if (tokens.empty() || tokens[0] == "null") return nullptr;
TreeNode* root = new TreeNode(std::stoi(tokens[0]));
std::queue<TreeNode*> q;
q.push(root);
size_t i = 1;
while (!q.empty() && i < tokens.size()) {
TreeNode* curr = q.front();
q.pop();
if (i < tokens.size()) {
if (tokens[i] != "null") {
curr->left = new TreeNode(std::stoi(tokens[i]));
q.push(curr->left);
}
i++;
}
if (i < tokens.size()) {
if (tokens[i] != "null") {
curr->right = new TreeNode(std::stoi(tokens[i]));
q.push(curr->right);
}
i++;
}
}
return root;
}
void freeTree(TreeNode* root) {
if (!root) return;
freeTree(root->left);
freeTree(root->right);
delete root;
}
std::vector<int> rightSideView(TreeNode* root) {
std::vector<int> res;
if (!root) return res;
std::queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
size_t sz = q.size();
for (size_t i = 0; i < sz; i++) {
TreeNode* node = q.front();
q.pop();
if (i == sz - 1) {
res.push_back(node->val);
}
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return res;
}
int main() {
std::vector<std::string> tokens;
std::string token;
while (std::cin >> token) {
tokens.push_back(token);
}
TreeNode* root = buildTree(tokens);
auto res = rightSideView(root);
for (size_t i = 0; i < res.size(); i++) {
std::cout << (i == 0 ? "" : " ") << res[i];
}
std::cout << "\n";
freeTree(root);
return 0;
}