1.二叉树
1.1给定先序遍历建立二叉树
原题链接
#include<iostream>
using namespace std;
int position;
char ss[105];
struct TreeNode {
char cc;
TreeNode * leftNode;
TreeNode * rightNode;
TreeNode(char c):cc(c),leftNode(NULL),rightNode(NULL){
}
};
TreeNode * build()
{
char x=ss[position++];
if(x=='#')
return NULL;
TreeNode * root=new TreeNode(x);
root->leftNode=build();
root->rightNode=build();
return root;
}
void InOrder(TreeNode * root)
{
if(!root)
return;
InOrder(root->leftNode);
printf("%c ",root->cc);
InOrder(root->rightNode);
}
int main()
{
scanf("%s",ss);
TreeNode * root=build();
InOrder(root);
return 0;
}
1.2给定一棵二叉树的前序遍历和中序遍历,求其后序遍历
原题链接
#include<iostream>
using namespace std;
#include<string>
struct TreeNode {
char cc;
TreeNode * leftNode;
TreeNode * rightNode;
TreeNode(char x):cc(x),leftNode(NULL),rightNode(NULL){
}
};
TreeNode * build(string str1,string str2)
{
if(!str1.size())
return NULL;
char xx=str1[0];
int loc=str2.find(xx);
TreeNode * root=new TreeNode(xx);
root->leftNode=build(str1.substr(1,loc+1-1),str2.substr(0,loc));
root->rightNode=build(str1.substr(loc+1),str2.substr(loc+1));
return root;
}
void PosOrder(TreeNode * root)
{
if(root==NULL)
return ;
PosOrder(root->leftNode);
PosOrder(root->rightNode);
printf("%c",root->cc);
}
int main()
{
string str1;
string str2;
while(getline(cin,str1))
{
getline(cin,str2);
TreeNode * root=build(str1,str2);
PosOrder(root);
printf("\n");
}
return 0;
}
1.3、给定二叉树的后序遍历和中序遍历,请你输出它的层序遍历
原题链接
#include<iostream>
using namespace std;
#include<queue>
#include<string>
const int N=50;
int a[N];
int b[N];
struct TreeNode {
int cc;
TreeNode * leftNode;
TreeNode * rightNode;
TreeNode(char x):cc(x),leftNode(NULL),rightNode(NULL){
}
};
queue<TreeNode *> qq;
int top;
TreeNode * build(int beg1,int end1,int beg2,int end2)
{
if(beg1>end1)
return NULL;
int xx=a[end1];
int loc;
for(int i=beg2;i<=end2;i++)
{
if(b[i]==xx)
{
loc=i;
break;
}
}
TreeNode * root=new TreeNode(xx);
root->leftNode=build(beg1,loc-beg2-1+beg1,beg2,loc-1);
root->rightNode=build(loc-beg2-1+beg1+1,end1-1,loc+1,end2);
return root;
}
void LevOrder(TreeNode * root)
{
if(!root)
return;
qq.push(root);
while(!qq.empty())
{
TreeNode * tt=qq.front();
qq.pop();
printf("%d ",tt->cc);
if(tt->leftNode)
qq.push(tt->leftNode);
if(tt->rightNode)
qq.push(tt->rightNode);
}
}
int main()
{
int n;
string str1="";
string str2="";
cin>>n;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
TreeNode * root=build(0,n-1,0,n-1);
LevOrder(root);
printf("\n");
return 0;
}
1.4、给定先序遍历和后序遍历,且是满二叉树,输出层次遍历
题目类似1.3
#include<iostream>
using namespace std;
#include<queue>
#include<string>
const int N=50;
int a[N];
int b[N];
struct TreeNode {
int cc;
TreeNode * leftNode;
TreeNode * rightNode;
TreeNode(char x):cc(x),leftNode(NULL),rightNode(NULL){
}
};
queue<TreeNode *> qq;
int top;
TreeNode * build(int beg1,int end1,int beg2,int end2)
{
if(beg1==end1)
{
int xx=a[end1];
TreeNode * root=new TreeNode(xx);
root->leftNode=NULL;
root->rightNode=NULL;
return root;
}
int xx=a[beg1];
int yy=a[beg1+1];
int loc;
for(int i=beg2;i<=end2;i++)
{
if(b[i]==yy)
{
loc=i;
break;
}
}
TreeNode * root=new TreeNode(xx);
root->leftNode=build(beg1+1,loc-beg2+beg1+1,beg2,loc);
root->rightNode=build(loc-beg2+beg1+2,end1,loc+1,end2-1);
return root;
}
void LevOrder(TreeNode * root)
{
if(!root)
return;
qq.push(root);
while(!qq.empty())
{
TreeNode * tt=qq.front();
qq.pop();
printf("%d ",tt->cc);
if(tt->leftNode)
qq.push(tt->leftNode);
if(tt->rightNode)
qq.push(tt->rightNode);
}
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
TreeNode * root=build(0,n-1,0,n-1);
LevOrder(root);
printf("\n");
return 0;
}