1.还原二叉树
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1000;
const int M = 35;
int f[N], s[N], c[N];
struct node {
node *l, *r;
char data;
};
//BUILD TREE
node *bt(char a[], char b[], int n) {
node *t;
int i;
if (!n)
return NULL;
else {
t = new node;
t->data = a[0];
for (i = 0; i <= n; i++) {
if (a[0] == b[i])
break;
}
t->l = bt(a + 1, b, i);
t->r = bt(a + i + 1, b + i + 1, n - i - 1);
}
return t;
}
//GET HIGHT
int gh(node *tree) {
int hr, hl, h;
if (tree) {
hl = gh(tree->l);
hr = gh(tree->r);
h = max(hr, hl);
h++;
}
return h;
}
int main() {
int n;
char a[55], b[55];
cin >> n >> a >> b;
node *tree = bt(a, b, n);
cout << gh(tree);
}
2.朋友圈
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 1000;
const int N = 35;
int f[M], s[M], c[M];
int find(int x) {
if (f[x] == x)
return x;
else
return f[x] = find(f[x]);
}
void fri(int x, int y) {
int a = find(x), b = find(y);
if (a != b)
f[a] = b;
}
int main() {
int n, m, x, a, b, i, ma = 0, sum = 0;
cin >> n >> m;
for (i = 1; i <= n + 5; i++)
f[i] = i;
while (m--) {
cin >> x;
for (i = 0; i < x; i++)
cin >> s[i];
for (i = 1; i < x; i++)
fri(s[0], s[i]);
}
for (i = 1; i <= n; i++) {
int y = find(i);
c[y]++;
ma = max(ma, c[y]);
}
cout << ma;
}
3.修理牧场
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, c, sum = 0, a;
priority_queue<int, vector<int>, greater<int>> q;
cin >> n;
while (n--) {
cin >> c;
q.push(c);
}
while (q.size() > 1) {
a = q.top();
q.pop();
a += q.top();
q.pop();
q.push(a);
sum += a;
}
cout << sum << endl;
}
4.树的遍历
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 1000;
const int N = 35;
int a[N], b[N];
int maxx = 0;
vector<int> xx[N];
int fun(int root, int start, int end, int num) {
int i;
if (start < 0 || start > end)
return 0;
xx[num].push_back(a[root]);
for (i = start; i <= end; i++) {
if (a[root] == b[i])
break;
}
maxx = max(maxx, num);
fun(root - 1 - end + i, start, i - 1, num + 1);
fun(root - 1, i + 1, end, num + 1);
}
int main() {
int n, i, j;
cin >> n;
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < n; i++)
cin >> b[i];
fun(n - 1, 0, n - 1, 0);
int flag = 0;
for (i = 0; i <= maxx; i++) {
for (j = 0; j < xx[i].size(); j++) {
if (flag)
cout << " ";
cout << xx[i][j];
flag = 1;
}
}
}
5.部落
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1000;
const int M = 35;
int f[N], s[N], c[N];
int find(int x) {
if (f[x] == x)
return x;
else
return f[x] = find(f[x]);
}
void fri(int x, int y) {
int a = find(x), b = find(y);
if (a != b)
f[a] = b;
}
int main() {
int n, m, x, a, b, ma = 0, sum = 0, count = 0, i;
cin >> n ;
for (i = 1; i <= n + 5; i++)
f[i] = i;
while (n--) {
cin >> x >> a;
ma = a;
for (i = 1; i < x; i++) {
cin >> b;
fri(a, b);
ma = max(a, b);
}
sum = max(ma, sum);
}
for (i = 1; i <= sum; i++)
c[find(i)]++;
for (i = 1; i <= sum; i++)
if (c[i] > 0)
count++;
cout << sum << " " << count << endl;
cin >> m;
while (m--) {
cin >> a >> b;
if (find(a) == find(b))
cout << "Y" << endl;
else
cout << "N" << endl;
}
}
6.交换二叉树中每个结点的左孩子和右孩子
#include <bits/stdc++.h>
using namespace std;
stack<char> q;
struct node {
node *l, *r;
char data;
};
//buildtree
void btree(node *&tree) { //改变
char ch;
cin >> ch;
if (ch == '#')
tree = NULL;
else {
tree = new node;
tree->data = ch;
btree(tree->l);
btree(tree->r);
}
}
//cout
void in(node *tree) {
if (tree) {
in(tree->l);
cout << tree->data;
q.push(tree->data); //堆栈交换左右儿砸
in(tree->r);
}
}
int main() {
node *tree = NULL;
btree(tree);
in(tree);
cout << endl;
while (!q.empty()) {
cout << q.top();
q.pop();
}
}
- 列出叶结点
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
int lchi[maxn], rchi[maxn], inCnt[maxn] = { 0 }, outCnt[maxn] = { 0 }, flag = 0;
void init(int n) {
for (int i = 0; i <= n; i++) lchi[i] = rchi[i] = -1;
}
void bfs(int root) {
queue<int>Q;
Q.push(root);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
if (outCnt[u] == 0) {
if (flag) printf(" ");
else flag = 1;
printf("%d", u);
}
if (lchi[u] != -1) Q.push(lchi[u]);
if (rchi[u] != -1) Q.push(rchi[u]);
}
}
int main() {
int n,root; cin >> n;
init(n);
for (int i = 0; i < n; i++) {
char u, v; cin >> u >> v;
if (u != '-') lchi[i] = u - '0', outCnt[i]++, inCnt[u - '0']++;
if (v != '-') rchi[i] = v - '0', outCnt[i]++, inCnt[v - '0']++;
}
for (int i = 0; i < n; i++) if (inCnt[i] == 0) root = i;
bfs(root);
}
8.建立与遍历二叉树
#include <bits/stdc++.h>
using namespace std;
struct node {
node *l, *r;
char data;
};
//buildtree
void btree(node *&tree) { //改变
char ch;
cin >> ch;
if (ch == '#')
tree = NULL;
else {
tree = new node;
tree->data = ch;
btree(tree->l);
btree(tree->r);
}
}
//cout
void show(node *tree) {
if (tree) {
show(tree->l);
cout << tree->data;
show(tree->r);
}
}
int main() {
node *t = NULL;
btree(t);
show(t);
}
9.完全二叉树的层序遍历
#include <iostream>
using namespace std;
const int MAX_SIZE = 31;
int tree[MAX_SIZE];
void lastOrder(int id, int n)
{
if (id <= n)
{
lastOrder(id * 2, n);
lastOrder(id * 2 + 1, n);
cin >> tree[id];
}
}
int main()
{
int n;
cin >> n;
lastOrder(1, n);
cout << tree[1];
for (int i = 2; i <= n; i++)
{
cout << " " << tree[i];
}
return 0;
}
不懂得可以私信我,随时解答。
程序有错误请私信我,以及时改正。感谢!