L2-004 这是二叉搜索树吗? (25 分)
- 由于这道题的权值不是distinct,说明有相同权值的点,在二叉搜索树中左子树的权值严格小于根节点,因此,对于二叉搜索树来说,在中序遍历中找根节点的位置应该从左开始第一个,而在二叉搜索树的镜像(中序遍历为从大到小),在中序遍历中找根节点的位置应该从右开始第一个
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e3 + 10;
int inorder[N], preorder[N];
int postorder[N], cnt;
bool build(int il, int ir, int pl, int pr, int type) {
if (il > ir) return true;
int root = preorder[pl];
int k;
if (!type) {
for (k = il; k <= ir; ++ k) {
if (inorder[k] == root) {
break;
}
}
if (k > ir) return false;
} else {
for (k = ir; k >= il; -- k) {
if (inorder[k] == root) {
break;
}
}
if (k < il) return false;
}
bool ok = true;
if (!build(il, k - 1, pl + 1, pl + 1 + k - 1 - il + 1 - 1, type)) ok = false;
if (!build(k + 1, ir, pl + 1 + k - 1 - il + 1 - 1 + 1, pr, type)) ok = false;
postorder[cnt ++ ] = root;
return ok;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++ i) {
cin >> preorder[i];
inorder[i] = preorder[i];
}
sort(inorder, inorder + n);
if (build(0, n - 1, 0, n - 1, 0)) {
cout << "YES" << endl;
for (int i = 0; i < n; ++ i) {
if (i != 0) cout << ' ';
cout << postorder[i];
}
} else {
reverse(inorder, inorder + n);
cnt = 0;
if (build(0, n - 1, 0, n - 1, 1)) {
cout << "YES" << endl;
for (int i = 0; i < n; ++ i) {
if (i != 0) cout << ' ';
cout << postorder[i];
}
} else {
cout << "NO";
}
}
}
L2-006 树的遍历 (25 分)
- 层序遍历需要从根节点开始,因此在建树过程中,返回值为root
- 由于保证权值distinct,因此,用pos数组来记录位置,就不用每次遍历去寻找位置
- 这里如果不用unordered_map改用数组会wa一个点和段错误一个点
- map中查找是否拥有这个键 count,如果不等于0说明有
- 注意k > il才有左儿子
#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 40;
int postorder[N], inorder[N];
int q[N];
unordered_map<int, int> l, r, pos;
int build(int il, int ir, int pl, int pr) {
int root = postorder[pr];
int k = pos[root];
if (k > il) l[root] = build(il, k - 1, pl, pl + k - 1 - il + 1 - 1);
if (k < ir) r[root] = build(k + 1, ir, pl + k - 1 - il + 1 - 1 + 1, pr - 1);
return root;
}
void bfs(int root) {
int hh = 0, tt = 0;
q[tt ++ ] = root;
while (hh <= tt) {
int t = q[hh ++ ];
if (l.count(t)) q[tt ++ ] = l[t];
if (r.count(t)) q[tt ++ ] = r[t];
}
for (int i = 0; i < tt; ++ i) {
if (i != 0) cout << ' ';
cout << q[i];
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++ i) {
cin >> postorder[i];
}
for (int i = 1; i <= n; ++ i) {
cin >> inorder[i];
pos[inorder[i]] = i;
}
int root = build(1, n, 1, n);
bfs(root);
}
L2-011 玩转二叉树 (25 分)
#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 40;
int inorder[N], preorder[N];
int q[N];
unordered_map<int, int> pos, l, r;
int build(int il, int ir, int pl, int pr) {
int root = preorder[pl];
int k = pos[root];
if (k > il) r[root] = build(il, k - 1, pl + 1, pl + 1 + k - 1 - il + 1 - 1);
if (k < ir) l[root] = build(k + 1, ir, pl + 1 + k - 1 - il + 1 - 1 + 1, pr);
return root;
}
void bfs(int root) {
int hh = 0, tt = 0;
q[tt ++ ] = root;
while (hh <= tt) {
int t = q[hh ++ ];
if (l.count(t)) q[tt ++ ] = l[t];
if (r.count(t)) q[tt ++ ] = r[t];
}
for (int i = 0; i < tt; ++ i) {
if (i != 0) cout << ' ';
cout << q[i];
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++ i) {
cin >> inorder[i];
pos[inorder[i]] = i;
}
for (int i = 1; i <= n; ++ i) {
cin >> preorder[i];
}
int root = build(1, n, 1, n);
bfs(root);
}
L2-026 小字辈 (25 分)
#include <iostream>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
int n;
vector<int> G[N];
int height;
vector<int> res;
void dfs1(int u, int h) {
height = max(height, h);
for (int i = 0; i < G[u].size(); ++ i) {
dfs1(G[u][i], h + 1);
}
}
void dfs2(int u, int h) {
if (h == height) {
res.push_back(u);
}
for (int i = 0; i < G[u].size(); ++ i) {
dfs2(G[u][i], h + 1);
}
}
int main() {
cin >> n;
int root;
for (int i = 1; i <= n; ++ i) {
int x;
cin >> x;
if (x == -1) {
root = i;
} else {
G[x].push_back(i);
}
}
dfs1(root, 1);
cout << height << endl;
dfs2(root, 1);
for (int i = 0; i < res.size(); ++ i) {
cout << res[i];
if (i != res.size() - 1) cout << ' ';
}
}
L2-020 功夫传人 (25 分)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int n;
double z, r;
vector<int> g[N];
int dedao[N];
double power[N];
double res;
void dfs(int u, int cur) {
if (dedao[u]) {
res += dedao[u] * power[cur] * z;
} else {
for (int i = 0; i < g[u].size(); ++ i) {
dfs(g[u][i], cur + 1);
}
}
}
int main() {
cin >> n >> z >> r;
r = 1 - 0.01 * r;
for (int i = 0; i < n; ++ i) {
int k;
cin >> k;
if (k == 0) {
int x;
cin >> x;
dedao[i] = x;
} else {
while (k -- ) {
int x;
cin >> x;
g[i].push_back(x);
}
}
}
power[0] = 1;
for (int i = 1; i < N; ++ i) {
power[i] = power[i - 1] * r;
}
dfs(0, 0);
cout << (int)res;
}
L2-035 完全二叉树的层序遍历 (25 分)
#include <iostream>
using namespace std;
const int N = 40;
int n;
int a[N], idx;
int tree[N];
void build(int u) {
if (u > n) {
return ;
}
build(u << 1);
build((u << 1) + 1);
tree[u] = a[idx ++];
}
int main() {
cin >> n;
for (int i = 0; i < n; ++ i) {
cin >> a[i];
}
build(1);
for (int i = 1; i <= n; ++ i) {
cout << tree[i];
if (i != n) cout << ' ';
}
}
L2-038 病毒溯源 (25 分)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e4 + 10;
vector<int> g[N];
int fa[N];
bool st[N];
int mx, lastNode;
void dfs(int u, int cur) {
if (cur > mx) {
mx = cur;
lastNode = u;
}
for (auto x : g[u]) {
dfs(x, cur + 1);
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++ i) {
int k;
cin >> k;
while (k -- ) {
int x;
cin >> x;
st[x] = true;
g[i].push_back(x);
fa[x] = i;
}
sort(g[i].begin(), g[i].end());
}
int root;
for (int i = 0; i < n; ++ i) {
if (!st[i]) {
root = i;
break;
}
}
dfs(root, 1);
cout << mx << endl;
vector<int> res;
int now = lastNode;
while (now != root) {
res.push_back(now);
now = fa[now];
}
res.push_back(root);
for (int i = (int)res.size() - 1; i >= 0; -- i) {
cout << res[i];
if (i != 0) cout << ' ';
}
}
L3-010 是否完全二叉搜索树 (30 分)
#include <iostream>
using namespace std;
const int N = 100;
int n;
int Tree[N];
void update(int root, int x) {
if (!Tree[root]) {
Tree[root] = x;
} else {
if (x > Tree[root]) {
update(root * 2, x);
} else {
update(root * 2 + 1, x);
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++ i) {
int x;
cin >> x;
update(1, x);
}
bool ok = true;
int mx = 0;
for (int i = 1; i < N; ++ i) {
if (Tree[i]) {
if (!ok) {
cout << ' ';
} else {
ok = false;
}
cout << Tree[i];
mx = max(mx, i);
}
}
cout << endl;
if (mx > n) {
cout << "NO";
} else {
cout << "YES";
}
}