1,交换二叉树的左右孩子;2,已知先序中序求后序 and 已知中序后序求先序;3,求组合数复习
直接写即可;
#include<bits/stdc++.h>
#define rep1(i,a,n) for( ll i=a;i<n;i++)
#define rep2(i,a,n) for( ll i=a;i<=n;i++)
#define per1(i,n,a) for( ll i=n;i>a;i--)
#define per2(i,n,a) for( ll i=n;i>=a;i--)
#define quick_cin() cin.tie(0),ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define endl "\n"
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
typedef struct BTnode
{
char data;
BTnode *Lchild,*Rchild;
}*BinTree;
void creatBT(BinTree &BT)
{
char c;
cin>>c;
if(c=='#')return;
if(BT==NULL)
{
BT=(BTnode*)malloc(sizeof(BTnode));
BT->data=c;
BT->Lchild=NULL;
BT->Rchild=NULL;
}
creatBT(BT->Lchild);
creatBT(BT->Rchild);
}
void midorder(BinTree &BT)
{
if(BT==NULL)return;
midorder(BT->Lchild);
cout<<BT->data;
midorder(BT->Rchild);
}
void jiaohuan(BinTree &BT)
{
if(BT)
{
swap(BT->Lchild,BT->Rchild);
jiaohuan(BT->Lchild);
jiaohuan(BT->Rchild);
}
}
signed main()
{
quick_cin();
BTnode *BT=NULL;
creatBT(BT);
midorder(BT);
cout<<endl;
jiaohuan(BT);
midorder(BT);
return 0;
}
#include<bits/stdc++.h>
#define rep1(i,a,n) for( ll i=a;i<n;i++)
#define rep2(i,a,n) for( ll i=a;i<=n;i++)
#define per1(i,n,a) for( ll i=n;i>a;i--)
#define per2(i,n,a) for( ll i=n;i>=a;i--)
#define quick_cin() cin.tie(0),ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define endl "\n"
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
const int N=1e3+10;
char pre[N],mid[N],post[N];
int n;
void postorder(int l1,int r1,int l2,int r2)
{
if(l1>r1)return;
int i;
for( i=l2;mid[i]!=pre[l1];i++);
postorder(l1+1,r1-r2+i,l2,i-1);
postorder(r1-r2+i+1,r1,i+1,r2);
cout<<pre[l1];
}
void preorder(int l1,int r1,int l2,int r2)
{
if(l1>r1)return;
int i;
for(i=l2;mid[i]!=post[r1];i++);
cout<<post[r1];
preorder(l1,r1-r2+i-1,l2,i-1);
preorder(r1-r2+i,r1-1,i+1,r2);
}
signed main()
{
quick_cin();
cin>>n>>pre>>mid>>post;
postorder(0,n-1,0,n-1);
cout<<endl;
preorder(0,n-1,0,n-1);
return 0;
}
3,组合数
递推公式
c[ a , b ] = c[ a-1 , b] + c[ a-1 , b -1 ];
初始化注意rep2(i,0,N-1)一定是N-1,不然会报错!(因为是二维数组把,所以N-1出错?)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( ll i=a;i<n;i++)
#define rep2(i,a,n) for( ll i=a;i<=n;i++)
#define per1(i,n,a) for( ll i=n;i>a;i--)
#define per2(i,n,a) for( ll i=n;i>=a;i--)
#define quick_cin() cin.tie(0),ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define endl "\n"
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
const int N=2e3+10,mod=1e9+7;
int c[N][N];
int n;
void init()
{
rep2(i,0,N-1)
{
rep2(j,0,i)
{
if(!j)c[i][j]=1;
else c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
}
}
signed main()
{
quick_cin();
cin>>n;
init();
while(n--)
{
int a,b;
cin>>a>>b;
cout<<c[a][b]<<endl;
}
return 0;
}