二叉树——前中构造
来源:互联网 发布:阿里云域名无需备案 编辑:程序博客网 时间:2024/06/08 10:55
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0x3f3f3f3f
#define mod 123456789
#define N 70
typedef long long ll;
const int M=1005;
int n,m;
int cnt;
int sx,sy,sz;
int mp[M][12];
int pa[M*10],rankk[M];
int head[M*6],vis[M*10];
double dis[M][10];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
vector<int> g[M*10];
int dp[2005][2005];
int has[100005];
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
bool isrun(int year)
{if((year%4==0&&year%100!=0)||(year%400==0))
return true;return false;
}
void getpri()
{
ll i;int j;cnt=0;
memset(isprime,false,sizeof(isprime));
for(i=2; i<1000000LL; i++)
{
if(!isprime[i])prime[cnt++]=i;
for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
{
isprime[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}
}
ll qk_mul(ll a,ll b,ll mo)
{
ll t=0;
while(b)
{
if(b&1)
t=(t+a)%mo;
a=(a<<1)%mo;
b>>=1;
}
t%=mo;return t;
}
ll qk_mod(ll a,ll b,ll mo)
{
ll ans=1;
while(b)
{
if(b&1) ans=qk_mul(ans,a,mo);
a=qk_mul(a,a,mo);
b>>=1;
}
ans%=mo;return ans;
}
int gcd(int a,int b)
{return b == 0 ? a : gcd(b, a%b);}
int findx(int t)
{
if(t!=pa[t])
pa[t]=findx(pa[t]);
return pa[t];
}
void unionx(int x,int y)
{
x=findx(x);
y=findx(y);
if(x!=y){pa[y]=x;}
}
int in[50],pre[50];
struct node{
int data;
node *left,*right;
}*roo;
node *create(int *pre,int *in,int len)
{
int i;
if(len<=0)return NULL;
node *root=(node *)malloc(sizeof(node));
root->data=*pre;
++cnt;
if(cnt==1)roo=root;
for(i=0;i<len;i++)
if(in[i]==*pre)break;
root->left=create(pre+1,in,i);
root->right=create(pre+1+i,in+i+1,len-i-1);
return root;
}
queue<node *> qu;
void print(node *root)
{
cnt=0;
while(!qu.empty())qu.pop();
qu.push(root);
while(!qu.empty())
{
node *t=qu.front();
qu.pop();
pa[cnt++]=t->data;
if(t->right!=NULL)qu.push(t->right);
if(t->left!=NULL)qu.push(t->left);
}
for(int i=0;i<cnt;i++)
{
if(i)printf(" ");
printf("%d",pa[i]);
}
printf("\n");
}
int main()
{
int i,j,t,k;
scanf("%d",&t);
for(i=0;i<t;i++)scanf("%d",&in[i]);
for(i=0;i<t;i++)scanf("%d",&pre[i]);
node *root=create(pre,in,t);
print(root);
return 0;
}
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0x3f3f3f3f
#define mod 123456789
#define N 70
typedef long long ll;
const int M=1005;
int n,m;
int cnt;
int sx,sy,sz;
int mp[M][12];
int pa[M*10],rankk[M];
int head[M*6],vis[M*10];
double dis[M][10];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
vector<int> g[M*10];
int dp[2005][2005];
int has[100005];
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
bool isrun(int year)
{if((year%4==0&&year%100!=0)||(year%400==0))
return true;return false;
}
void getpri()
{
ll i;int j;cnt=0;
memset(isprime,false,sizeof(isprime));
for(i=2; i<1000000LL; i++)
{
if(!isprime[i])prime[cnt++]=i;
for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
{
isprime[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}
}
ll qk_mul(ll a,ll b,ll mo)
{
ll t=0;
while(b)
{
if(b&1)
t=(t+a)%mo;
a=(a<<1)%mo;
b>>=1;
}
t%=mo;return t;
}
ll qk_mod(ll a,ll b,ll mo)
{
ll ans=1;
while(b)
{
if(b&1) ans=qk_mul(ans,a,mo);
a=qk_mul(a,a,mo);
b>>=1;
}
ans%=mo;return ans;
}
int gcd(int a,int b)
{return b == 0 ? a : gcd(b, a%b);}
int findx(int t)
{
if(t!=pa[t])
pa[t]=findx(pa[t]);
return pa[t];
}
void unionx(int x,int y)
{
x=findx(x);
y=findx(y);
if(x!=y){pa[y]=x;}
}
int in[50],pre[50];
struct node{
int data;
node *left,*right;
}*roo;
node *create(int *pre,int *in,int len)
{
int i;
if(len<=0)return NULL;
node *root=(node *)malloc(sizeof(node));
root->data=*pre;
++cnt;
if(cnt==1)roo=root;
for(i=0;i<len;i++)
if(in[i]==*pre)break;
root->left=create(pre+1,in,i);
root->right=create(pre+1+i,in+i+1,len-i-1);
return root;
}
queue<node *> qu;
void print(node *root)
{
cnt=0;
while(!qu.empty())qu.pop();
qu.push(root);
while(!qu.empty())
{
node *t=qu.front();
qu.pop();
pa[cnt++]=t->data;
if(t->right!=NULL)qu.push(t->right);
if(t->left!=NULL)qu.push(t->left);
}
for(int i=0;i<cnt;i++)
{
if(i)printf(" ");
printf("%d",pa[i]);
}
printf("\n");
}
int main()
{
int i,j,t,k;
scanf("%d",&t);
for(i=0;i<t;i++)scanf("%d",&in[i]);
for(i=0;i<t;i++)scanf("%d",&pre[i]);
node *root=create(pre,in,t);
print(root);
return 0;
}
0 0
- 二叉树——前中构造
- LINTCODE——前序遍历和中序遍历树构造二叉树
- 树—根据中序遍历和后序遍历(或者前序和中序)构造二叉树
- 根据前序和中序遍历构造二叉树
- 根据前序和中序遍历构造二叉树
- C++根据前序和中序构造二叉树
- 前序和中序遍历构造二叉树
- 给出中序和后序遍历,构造二叉树以及给出前序和中序构造二叉树
- 根据二叉树的前序遍历和中序遍历构造二叉树
- 二叉树专题-根据前序和中序序列构造二叉树
- 二叉树:已知前序&&中序或中序&&后序构造树
- 二叉树序列---根据后序和中序系列或前序和中序序列构造二叉树
- 【工程】二叉树已知前序/中序的顺序,构造树的递归等实现
- Lintcode前序遍历和中序遍历树构造二叉树
- 前序遍历和中序遍历树构造二叉树
- 前序遍历和中序遍历树构造二叉树 查看运行结果
- 前序遍历和中序遍历树构造二叉树
- 题目:前序遍历和中序遍历树构造二叉树
- iOS透明导航栏的平滑过渡(进阶版)
- 光流法简单介绍
- 突发|百度首席科学家吴恩达巡捕离职百度
- 【简单】数字翻转
- 新手向--C++学习手札
- 二叉树——前中构造
- 项目在迭代过程中 清理 没有关联的资源 android studio 应用 清理资源 lint
- 生成随机数
- 【高中数学】三角形面积公式
- Linux Shell编程入门
- RecyclerView的基本使用
- 输入一个1~9之间的数字 ,对 1到该数之间所有的数进行全排序(深度优先搜索的基本模型)
- pmbd安装
- 电脑上传文件至GitHub全过程