3.29周六晚上树上问题专题测总结

· · 个人记录

总结

1.树上问题专题-找树根和孩子

思路

比较简单的模板题,因为找孩子最多的结点 max,用孩子表示法即可

Ac Code

#include<bits/stdc++.h>
using namespace std;
int n,m;
int f[101];
vector<int> q[101];
int maxn=0;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("T1.in","r",stdin);
//  freopen("T1.out","w",stdout);
    cin>>n>>m;
    while(m--){
        int x,y;
        cin>>x>>y;
        f[y]++;
        q[x].push_back(y);
    }
    for(int i=1;i<=n;i++){
        if(f[i]==0){
            cout<<i<<endl;
            break;
        }
    }
    for(int i=1;i<=n;i++){
        maxn=max(maxn,int(q[i].size()));
    }
    for(int i=1;i<=n;i++){
        if(q[i].size()==maxn){
            cout<<i<<endl;
            sort(q[i].begin(),q[i].end());
            for(int j=0;j<q[i].size();j++){
                cout<<q[i][j]<<' ';
            }
            cout<<endl;
            break;
        }
    }
    return 0;
} 

2.树上问题专题-中、后序遍历求前序遍历

思路

通过中序遍历和后序遍历找先序遍历,只需要递归输出根,并拆出左子树和右子树,就可以得到先序遍历

Ac Code

#include<bits/stdc++.h>
using namespace std;
string a,b;
void dfs(string back,string mid){
    int lens=back.length();
    if(!lens){
        return;
    }
    cout<<back[lens-1];
    int pos=mid.find(back[lens-1]);
    dfs(back.substr(0,pos),mid.substr(0,pos));
    dfs(back.substr(pos,lens-pos-1),mid.substr(pos+1,lens-pos-1));
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("T2.in","r",stdin);
//  freopen("T2.out","w",stdout);
    cin>>a>>b;
    dfs(b,a);

    return 0;
} 
/*
ACBDFEG 
ABDCGEF

*/

3.树上问题专题-查找二叉树

思路

本题要求求解的是数值为x的位于中序遍历的几号位置。所以只需要记录每个点的权值,左孩子和右孩子,再按中序进行递归遍历即可

重点代码

记录每个点的权值,左孩子和右孩子

struct node{
    int value;
    int l;
    int r;
}q[maxn];

Ac Code

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
struct node{
    int value;
    int l;
    int r;
}q[maxn];
int n,x;
int ans=0;
void dfs(int u){
    if(q[u].l){
        dfs(q[u].l);
    }
    ans++;
    if(x==q[u].value){
        cout<<ans<<endl;
        exit(0);
    }
    if(q[u].r){
        dfs(q[u].r);
    }
    return;
} 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("T3.in","r",stdin);
//  freopen("T3.out","w",stdout);
    cin>>n>>x;
    for(int i=1;i<=n;i++){
        cin>>q[i].value>>q[i].l>>q[i].r;
    }
    dfs(1);
    return 0;
}

4.树上问题专题-FBI树

思路

每次将区间拆分一半,根据区间的拆分情况得到后续的字符表示。

重点代码

每次拆分一半,通过拆除的字符串得出F或B或I

void dfs(string s){
    if(s.size()==1){
        cout<<getfbi(s);
        return;
    }
    int len=s.size();
    dfs(s.substr(0,len/2));//拆出左子树
    dfs(s.substr(len /2));//拆出右子树
    cout << getfbi(s);//输出根
    return;
}

Ac Code

#include <bits/stdc++.h>
using namespace std;
char getfbi(string s){
    int cnt=0;
    for(int i=0;i<s.size(); i++){
        if(s[i] =='0'){
            cnt++;
        }

    }
    if(cnt==0){
        return 'I';
    }
    else if(cnt ==s.size()){
        return 'B';
    }

    return 'F';
} 

void dfs(string s){
    if(s.size()==1){
        cout<<getfbi(s);
        return;
    }
    int len=s.size();
    dfs(s.substr(0,len/2));
    dfs(s.substr(len /2));
    cout << getfbi(s);
    return;
}
int main(){
    int n;
    string s;
    cin >> n >> s;
    dfs(s);
    return 0;
}

5.树上问题专题-扩展二叉树

思路

首先要知道不能通过先序遍历得出完整的二叉树,但是能通过dfs得出中序遍历和后序遍历

重点代码

无

Ac Code

#include<bits/stdc++.h>
using namespace std;
string s;
int pos=0;
void dfs1(){
    if(s[pos]=='.'){
        pos++;//字数结束,pos++,跳到下一个节点
        return;
    }
    char c = s[pos];
    pos++;
    dfs1();//左边
    cout<<c;//输出根
    dfs1();//右边
    return;
}
//同理可得后序遍历
void dfs2(){
    if(s[pos]=='.'){
        pos++;
        return;
    }
    char c = s[pos];
    pos++;
    dfs2();
    dfs2();
    cout<<c;
    return;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("T5.in","r",stdin);
//  freopen("T5.out","w",stdout);
    cin>>s;
    dfs1();
    pos=0;//记得清零
    cout<<endl;
    dfs2();
    return 0;
}

6.树上问题专题-对称二叉树

思路

由图可知,在同一深度,右子树的左边(右边)和左子树的右边(左边)相等,则满足对称

重点代码

存储数据

//存一个点的左节点,右节点,点权和以它为根节点的对称子树的大小
struct node{
    int lt,rt,val;
    long long size;
}a[N];

判断是否符合对称

bool check(int x,int y){
    if(x==-1&&y==-1){//左右孩子不存在 
        return true;
    }
    if(x!=-1&&y!=-1&&a[x].val==a[y].val){//左右孩子都存在,且权值相等 
        if(check(a[x].lt,a[y].rt)==true&&check(a[x].rt,a[y].lt)==true){
            return true;
        }
    }
    return false;
}

Ac Code

#include<bits/stdc++.h>
using namespace std;
const int N=1E6+5;
struct node{
    int lt,rt,val;
    long long size;
}a[N];
void get_size(int cur){
    a[cur].size=1;//子树大小最小为1 
    if(a[cur].lt!=-1){//左孩子存在 
        get_size(a[cur].lt);
        a[cur].size+=a[a[cur].lt].size;
    }
    if(a[cur].rt!=-1){//右孩子存在 
        get_size(a[cur].rt);
        a[cur].size+=a[a[cur].rt].size;
    }
    return;
}
bool check(int x,int y){
    if(x==-1&&y==-1){//左右孩子不存在 
        return true;
    }
    if(x!=-1&&y!=-1&&a[x].val==a[y].val){//左右孩子都存在,且权值相等 
        if(check(a[x].lt,a[y].rt)==true&&check(a[x].rt,a[y].lt)==true){
            return true;
        }
    }
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("T6.in","r",stdin);
//  freopen("T6.out","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].val;
    }
    for(int i=1;i<=n;i++){
        cin>>a[i].lt>>a[i].rt;
    }
    get_size(1);
    long long ans=0;
    for(int i=1;i<=n;i++){
        if(check(a[i].lt,a[i].rt)==true){//对称 
            ans=max(ans,a[i].size);//统计答案
        }
    }
    cout<<ans;
    return 0;
}