题意 :
思路 :
哈希表建二叉树
语法 :
#include
#include
using namespace std;
const int N = 40;
int n;
int inorder[N], postorder[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));
if (k < ir) r[root] = build(k + 1, ir, pl + (k - 1 - il) + 1, pr - 1);
return root;
}
void bfs(int root)
{
int hh = 0, tt = 0;
q[0] = root;
while(hh <= tt)
{
int t = q[hh ++ ];
if (l.count(t)) q[ ++ tt] = l[t];
if (r.count(t)) q[ ++ tt] = r[t];
}
cout << q[0];
for (int i = 1; i < n; i ++ ) cout << ' ' << q[i];
cout << endl;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> postorder[i];
for (int i = 0; i < n; i ++ )
{
cin >> inorder[i];
pos[inorder[i]] = i;
}
int root = build(0, n - 1, 0, n - 1);
bfs(root);
return 0;
}
题意 :
思路 :
#include
#include
using namespace std;
const int N = 1e4 + 10, M = 2 * N;
int n;
int h[N], e[M], ne[M], idx;
int fa[N];
int find(int x)
{
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
int dfs(int u, int father)
{
int depth = 0;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (j == father) continue;
depth = max(depth, dfs(j, u) + 1);
}
return depth;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) h[i] = -1, fa[i] = i;
int k = n;
for (int i = 1, u, v; i <= n - 1 && cin >> u >> v; i ++ )
{
if (find(u) != find(v))
{
k -- ;
fa[find(u)] = find(v);
}
add(u, v), add(v, u);
}
if (k > 1) printf("Error: %d components\n", k);
else
{
vector<int> nodes;
int max_depth = -1, depth;
for (int i = 1; i <= n; i ++ )
{
depth = dfs(i, -1);
if (depth > max_depth)
{
max_depth = depth;
nodes.clear();
nodes.push_back(i);
}
else if (depth == max_depth)
nodes.push_back(i);
}
for (auto v : nodes) cout << v << endl;
}
}
题意 :
思路 :
语法 :
#include
#include
using namespace std;
const int N = 1e3 + 10;
int n;
int preorder[N], inorder[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) // BST
{
for (k = il; k <= ir; k ++ )
if (inorder[k] == root)
break;
if (k > ir) return false;
}
else // mirror
{
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, k - 1 - il + pl + 1, type)) ok = false;
if (!build(k + 1, ir, k - 1 - il + pl + 1 + 1, pr, type)) ok = false;
postorder[cnt ++ ] = root;
return ok;
}
int main()
{
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))
{
puts("YES");
cout << postorder[0];
for (int i = 1; i < n; i ++ ) cout << ' ' << postorder[i];
cout << endl;
}
else
{
reverse(inorder, inorder + n); // 翻转中序遍历序列
cnt = 0; // 重新建立后序遍历
if (build(0, n - 1, 0, n - 1, 1))
{
puts("YES");
cout << postorder[0];
for (int i = 1; i < n; i ++ ) cout << ' ' << postorder[i];
cout << endl;
}
else
{
puts("NO");
}
}
}