22.4.13

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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dull丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值