L2-004 这是二叉搜索树吗? (25 分)
思路:按给出的先序遍历序列的顺序建立二叉搜索树,建完后求出此二叉搜索树的先序遍历,若跟之前的序列一样说明之前的序列就是他的先序遍历序列。对镜像也是这么处理。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[1005];
struct node{
int val;
node* l;
node* r;
};
node* build(node* root,int val)
{
if(root==NULL)
{
root=new node(); //分配空间
root->val=val;
}
else if(root->val <= val)
root->r=build(root->r,val);
else
root->l=build(root->l,val);
return root;
}
node* build_reverse(node* root,int val)
{
if(root==NULL)
{
root=new node(); //分配空间
root->val=val;
}
else if(root->val <= val)
root->l=build_reverse(root->l,val);
else
root->r=build_reverse(root->r,val);
return root;
}
vector<int> v;
void preshow(node* root)
{
if(root==NULL)
return;
v.push_back(root->val);
preshow(root->l);
preshow(root->r);
}
void houshow(node* root)
{
if(root==NULL)
return;
houshow(root->l);
houshow(root->r);
v.push_back(root->val);
}
int main()
{
int n;
cin>>n;
node* root=NULL;
for(int i=0;i<n;i++)
{
cin>>a[i];
root=build(root,a[i]);
}
preshow(root);
int flag=0;
for(int i=0;i<v.size();i++)
{
if(a[i]!=v[i])
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<"YES"<<endl;
v.clear();
houshow(root);
for(int i=0;i<v.size();i++)
{
if(i==v.size()-1)
cout<<v[i]<<endl;
else
cout<<v[i]<<" ";
}
}
else
{
flag=0;
root=NULL;
v.clear();
for(int i=0;i<n;i++)
root=build_reverse(root,a[i]);
preshow(root);
for(int i=0;i<v.size();i++)
{
if(a[i]!=v[i])
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<"YES"<<endl;
v.clear();
houshow(root);
for(int i=0;i<v.size();i++)
{
if(i==v.size()-1)
cout<<v[i]<<endl;
else
cout<<v[i]<<" ";
}
}
else
cout<<"NO"<<endl;
}
}
L2-006 树的遍历 (25 分)
思路:基本数据结构思路,由后序遍历和中序遍历建树然后输出层序遍历
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int in[100],post[100];
struct node{
int val;
node* l;
node* r;
};
node* build(node* root,int l1,int r1,int l2,int r2)
{
if(l1>r1)
return NULL;
if(root==NULL)
{
root=new node(); //分配空间
root->val=post[r2];
}
int p=l1;
while(in[p]!=root->val)
p++;
int cnt=p-l1;
root->l=build(root->l,l1,p-1,l2,l2+cnt-1);
root->r=build(root->r,p+1,r1,l2+cnt,r2-1);
return root;
}
vector<int> v;
void bfs(node* root)
{
queue<node*> q;
q.push(root);
while(!q.empty())
{
node* u=q.front();
q.pop();
v.push_back(u->val);
if(u->l!=NULL)
q.push(u->l);
if(u->r!=NULL)
q.push(u->r);
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>post[i];
for(int i=1;i<=n;i++)
cin>>in[i];
node* root=NULL;
root=build(root,1,n,1,n);
bfs(root);
for(int i=0;i<v.size();i++)
{
if(i==v.size()-1)
cout<<v[i]<<endl;
else
cout<<v[i]<<" ";
}
}
L2-011 玩转二叉树 (25 分)
思路:给出树的先序遍历和中序遍历建树,然后输出层序遍历,只不过在建树的过程中要翻转一下,左子树跟右子树互换
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int in[100],pre[100];
struct node{
int val;
node* l;
node* r;
};
node* build(node* root,int l1,int r1,int l2,int r2)
{
if(l1>r1)
return NULL;
if(root==NULL)
{
root=new node();
root->val=pre[l2];
}
int p=l1;
while(in[p]!=root->val)
p++;
int cnt=p-l1;
root->l=build(root->l,p+1,r1,l2+cnt+1,r2); //按题目要求此处翻转
root->r=build(root->r,l1,p-1,l2+1,l2+cnt);
return root;
}
vector<int> v;
void bfs(node* root)
{
queue<node*> q;
q.push(root);
while(!q.empty())
{
node* u=q.front();
q.pop();
v.push_back(u->val);
if(u->l!=NULL)
q.push(u->l);
if(u->r!=NULL)
q.push(u->r);
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>in[i];
for(int i=1;i<=n;i++)
cin>>pre[i];
node* root=NULL;
root=build(root,1,n,1,n);
bfs(root);
for(int i=0;i<v.size();i++)
{
if(i==v.size()-1)
cout<<v[i]<<endl;
else
cout<<v[i]<<" ";
}
}