DS二叉树–二叉树构建与遍历(含代码框架)
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct Tr
{
char ch;
int l, r;
}tr[N];
int cnt = 0, n, idx = 0;
char s[N];
int dfs()
{
idx ++;
if(idx == n || s[idx] == '0') return 0;
int cur = ++ cnt;
tr[cur].ch = s[idx];
tr[cur].l = dfs(), tr[cur].r = dfs();
return cur;
}
void pre(int i)
{
if(!i) return;
cout << tr[i].ch;
pre(tr[i].l);
pre(tr[i].r);
}
void mid(int i)
{
if(!i) return;
mid(tr[i].l);
cout << tr[i].ch;
mid(tr[i].r);
}
void post(int i)
{
if(!i) return;
post(tr[i].l);
post(tr[i].r);
cout << tr[i].ch;
}
int main()
{
#ifdef ZY
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t; cin >> t;
while(t --)
{
cin >> s;
n = strlen(s), cnt = 0, idx = -1;
dfs();
pre(1); puts(""); mid(1); puts(""); post(1); puts("");
}
return 0;
}
DS二叉树——二叉树之父子结点
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct Tr
{
char ch;
int l, r, fa;
}tr[N];
int cnt = 0, n, idx = 0;
char s[N];
int dfs()
{
idx ++;
if(idx == n || s[idx] == '0') return 0;
int cur = ++ cnt;
tr[cur].ch = s[idx];
tr[cur].l = dfs(), tr[cur].r = dfs();
tr[tr[cur].l].fa = cur, tr[tr[cur].r].fa = cur;
return cur;
}
void pre(int i)
{
if(!i) return;
if(!tr[i].l && !tr[i].r) cout << tr[i].ch << ' ';
pre(tr[i].l), pre(tr[i].r);
}
void pre_fa(int i)
{
if(!i) return;
if(!tr[i].l && !tr[i].r) cout << tr[tr[i].fa].ch << ' ';
pre_fa(tr[i].l), pre_fa(tr[i].r);
}
int main()
{
#ifdef ZY
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t; cin >> t;
while(t --)
{
cin >> s;
n = strlen(s), cnt = 0, idx = -1;
dfs();
pre(1); puts(""); pre_fa(1); puts("");
}
return 0;
}
DS树–二叉树高度
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct Tr
{
int l, r, height;
}tr[N];
int cnt = 0, n, idx = 0, height = 0;
char s[N];
int dfs()
{
idx ++;
if(idx == n || s[idx] == '0') return 0;
int cur = ++ cnt;
tr[cur].l = dfs(), tr[cur].r = dfs();
if(tr[cur].l) tr[cur].height = tr[tr[cur].l].height + 1;
if(tr[cur].r) tr[cur].height = max(tr[cur].height, tr[tr[cur].r].height + 1);
if(!tr[cur].l && !tr[cur].r) tr[cur].height = 1;
height = max(tr[cur].height, height);
return cur;
}
int main()
{
#ifdef ZY
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t; cin >> t;
while(t --)
{
cin >> s;
n = strlen(s), cnt = 0, idx = -1, height = 0;
dfs();
cout << height << endl;
}
return 0;
}
DS二叉树——二叉树之数组存储
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], n;
void dfs(int i)
{
if(i > n || !a[i]) return;
cout << a[i] << ' ';
dfs(i * 2), dfs(i * 2 + 1);
}
int main()
{
#ifdef ZY
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t; cin >> t;
while(t --)
{
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
dfs(1); puts("");
}
return 0;
}
DS二叉树–叶子数量
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int idx, leaves, n;
char s[N];
int dfs()
{
idx ++;
if(idx == n || s[idx] == '0') return 0;
int l = dfs(), r = dfs();
if(!l && !r) leaves ++;
return 1;
}
int main()
{
#ifdef ZY
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t; cin >> t;
while(t --)
{
cin >> s;
n = strlen(s), idx = -1, leaves = 0;
dfs();
cout << leaves << endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
char s[N];
int height, leaves, sum, n, idx, num[N], leaves_idx;
int dfs()
{
idx ++;
if(idx == n || s[idx] == '0') return 0;
height ++;
int cur_height = height;
bool l = dfs(), r = dfs();
if(!l && !r) sum += num[++ leaves_idx] * (cur_height - 1);
height --;
return 1;
}
int main()
{
#ifdef ZY
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t; cin >> t;
while(t --)
{
cin >> s;
n = strlen(s), idx = -1, height = 0, leaves = 0, sum = 0, leaves_idx = 0;
cin >> leaves;
for(int i = 1; i <= leaves; i ++) cin >> num[i];
dfs();
cout << sum << endl;
}
return 0;
}