整理日期按学习顺序编排,不代表原始发表时间。实际整理于 2026-10-24。

难度为阅读建议,可从 ACM 学习索引 按阶段阅读。

利用前序遍历的特性,如果左子树不空,下一个一定是左节点,不然就是#
因为是转中序遍历,就在两次遍历之间输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int k;
string str;

void dfs()
{
if (str[k] == '#')
{
k ++ ;
return;
}
char r = str[k ++ ]; // 根节点
dfs(); // 左子树
cout << r << ' ';
dfs(); // 右子树
}

int main()
{
cin >> str;
dfs();
return 0;
}