1.1020 Tree Traversals
题目大意:给定二叉树的后序遍历和中序遍历,请输出层序遍历。
只要给定中序遍历,然后搭配其余的一个遍历就可以唯一的确定一个唯一的二叉树。
算法的思想主要是进行左右子树的划分,一共有prel,prer,inl,inr四个比较关键的参数
使用指针的方式实现树的构建
#include <iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
//1020 Tree Traversals复习
/*
*/
using namespace std;
typedef struct Node
{
Node *lchild;
Node *rchild;
int date;
} node;
node *NewNode(int a)
{
node *root=new node;
root->date=a;
root->lchild=root->rchild=NULL;
return root;
}
int n;
vector<int> temp,post,in;
node *solve(int postl,int postr,int inl,int inr)
{
if(postl>postr)
{
return NULL;
}
node *root=NewNode(post[postl]);
int i;
for(i=inl;i<=inr;i++)
{
if(post[postl]==in[i])
{
break;
}
}
int rightnum=inr-i;
root->lchild=solve(postl+rightnum+1,postr,inl,i-1);
root->rchild=solve(postl+1,postl+rightnum,i+1,inr);
return root;
}
int k=0;
void layerorder(node *root)
{
queue<node*> q;
q.push(root);
while(!q.empty())
{
node *top=q.front();
q.pop();
if(k==0)
{
printf("%d",top->date);
k++;
}
else
{
printf(" %d",top->date);
}
if(top->lchild!=NULL)
{
q.push(top->lchild);
}
if(top->rchild!=NULL)
q.push(top->rchild);
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
int a;
scanf("%d",&a);
temp.push_back(a);
}
for(int i=n-1;i>=0;i--)
{
post.push_back(temp[i]);
}
for(int i=0;i<n;i++)
{
int a;
scanf("%d",&a);
in.push_back(a);
}
node *root=solve(0,n-1,0,n-1);
layerorder(root);
system("pause");
return 0;
}
1086 Tree Traversals Again
树可以由栈的操作唯一确定,将前序和中序提取出来就可以完成本题。
#include <iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<stack>
//1086 Tree Traversals Again复习
/*
*/
using namespace std;
typedef struct Node
{
Node *lchild;
Node *rchild;
int date;
} node;
node *NewNode(int a)
{
node *root=new node;
root->date=a;
root->lchild=root->rchild=NULL;
return root;
}
int n;
vector<int> in,pre;
node *creat(int prel,int prer,int inl,int inr)
{
if(prel>prer)
{
return NULL;
}
node *root=NewNode(pre[prel]);
int k;
for(k=inl;k<=inr;k++)
{
if(in[k]==pre[prel])
{
break;
}
}
int leftnum=k-inl;
root->lchild=creat(prel+1,prel+leftnum,inl,k-1);
root->rchild=creat(prel+leftnum+1,prer,k+1,inr);
return root;
}
int m=1;
void postorder(node *root)
{
if(root==NULL)
{
return;
}
postorder(root->lchild);
postorder(root->rchild);
if(m==1)
{
printf("%d",root->date);
m++;
}
else
{
printf(" %d",root->date);
}
}
int main()
{
cin>>n;
stack<int> sta;
for(int i=0;i<2*n;i++)
{
char s[20];
int num;
scanf("%s",s);
if(s[1]=='u')
{
scanf("%d",&num);
pre.push_back(num);
sta.push(num);
}
else
{
num=sta.top();
in.push_back(num);
sta.pop();
}
}
node *root=creat(0,n-1,0,n-1);
postorder(root);
/*
for(int i=0;i<2*n;i++)
{
string s;
int num;
cin>>s;
if(s[1]=='u')
{
scanf("%d",&num);
pre.push_back(num);
sta.push(num);
}
else
{
num=sta.top();
in.push_back(num);
sta.pop();
}
}*/
system("pause");
return 0;
}
1102 Invert a Binary Tree
由题目给定的条件创造一个树,然后输出树左右子树翻转后的层序和中序遍历。本次使用静态树来实现比较简单。
最后要寻找根节点,根节点一定满足不是其它节点的孩子。用一个bool型的数组判断即可。
需要注意的是scanf对于字符型会将换行符和空白符也吸收,需要进行getchar的吸收。
#include <iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<stack>
//1086 Tree Traversals Again复习
/*
*/
using namespace std;
const int inf=0x3fffffff;
const int maxn=20;
struct Node
{
int lchild;
int rchild;
} node[maxn];
int n;
bool judge[maxn]={false};
int c=1;
void layerorder(int root)
{
queue<int> q;
q.push(root);
while(!q.empty())
{
int top=q.front();
q.pop();
if(c==1)
{
c++;
printf("%d",top);
}
else
{
printf(" %d",top);
}
if(node[top].rchild!=inf)
q.push(node[top].rchild);
if(node[top].lchild!=inf)
q.push(node[top].lchild);
}
}
int c1=1;
void inorder(int root)
{
if(root==inf)
{
return;
}
inorder(node[root].rchild);
if(c1==1)
{
c1++;
printf("%d",root);
}
else
{
printf(" %d",root);
}
inorder(node[root].lchild);
}
int main()
{
cin>>n;
getchar();
for(int i=0;i<n;i++)
{
char c1,c2;
scanf("%c %c",&c1,&c2);
if(c1=='-')
{
node[i].lchild=inf;
}
else
{
int w=c1-'0';
node[i].lchild=w;
judge[w]=true;
}
if(c2=='-')
{
node[i].rchild=inf;
}
else
{
int w=c2-'0';
node[i].rchild=w;
judge[w]=true;
}
getchar();
}
int root;
for(root=0;root<n;root++)
{
if(judge[root]==false)
{
break;
}
}
layerorder(root);
cout<<endl;
inorder(root);
system("pause");
return 0;
}```
树的遍历
1079 Total Sales of Supply Chain
```cpp
#include <iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<stack>
//1086 Tree Traversals Again复习
/*
*/
using namespace std;
const int inf=0x3fffffff;
const int maxn=100010;
struct node
{
vector<int> child;
double price;
int number;
}tree[maxn];
int n;
double price,c;
double all=0;
void layerorder(int s)
{
queue<int> q;
q.push(s);
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=0;i<tree[top].child.size();i++)
{
int v=tree[top].child[i];
tree[v].price=tree[top].price+tree[top].price*c*0.01;
q.push(v);
}
}
}
void preorder(int s)
{
if(tree[s].child.size()==0)
{
all+=tree[s].number*tree[s].price;
}
for(int i=0;i<tree[s].child.size();i++)
{
preorder(tree[s].child[i]);
}
}
int main()
{
cin>>n>>price>>c;
tree[0].price=price;
for(int i=0;i<n;i++)
{
int k;
scanf("%d",&k);
for(int j=0;j<k;j++)
{
int input;
scanf("%d",&input);
tree[i].child.push_back(input);
}
if(k==0)
{
scanf("%d",&tree[i].number);
}
}
layerorder(0);
preorder(0);
printf("%0.1lf",all);
system("pause");
return 0;
}
1090 Highest Price in Supply Chain
#include <iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<stack>
//1086 Tree Traversals Again复习
/*
*/
using namespace std;
const int inf=0x3fffffff;
const int maxn=100010;
struct node
{
vector<int> child;
double price;
}tree[maxn];
int n;
double price,c;
double high=-5;
int num=0;
void layerorder(int s)
{
queue<int> q;
q.push(s);
tree[s].price=price;
while(!q.empty())
{
int top=q.front();
q.pop();
for(int j=0;j<tree[top].child.size();j++)
{
int v=tree[top].child[j];
tree[v].price=tree[top].price+tree[top].price*c*0.01;
q.push(v);
}
}
}
void preorder(int s)
{
if(tree[s].child.size()==0)
{
if(tree[s].price>high)
{
num=1;
high=tree[s].price;
}
else if(tree[s].price==high)
{
num++;
}
}
for(int i=0;i<tree[s].child.size();i++)
{
preorder(tree[s].child[i]);
}
}
int main()
{
cin>>n>>price>>c;
int root=0;
for(int i=0;i<n;i++)
{
int input;
scanf("%d",&input);
if(input!=-1)
{
tree[input].child.push_back(i);
}
else
{
root=i;
}
}
layerorder(root);
preorder(root);
printf("%0.2lf %d",high,num);
system("pause");
return 0;
}
1094 The Largest Generation
#include <iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<stack>
//1094 The Largest Generation
/*
*/
using namespace std;
const int inf=0x3fffffff;
const int maxn=100010;
struct node
{
vector<int> child;
int layer;
}tree[maxn];
struct Save
{
int layer;
int num;
Save()
{
layer=0;
num=0;
}
}save[maxn];
int n,m;
void layerorder(int s)
{
queue<int> q;
q.push(s);
tree[s].layer=1;
save[tree[s].layer].num++;
save[tree[s].layer].layer=1;
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=0;i<tree[top].child.size();i++)
{
int v=tree[top].child[i];
tree[v].layer=tree[top].layer+1;
q.push(v);
save[tree[v].layer].num++;
save[tree[v].layer].layer=tree[v].layer;
}
}
}
bool cmp(Save a,Save b)
{
return a.num>b.num;
}
int main()
{
cin>>n>>m;
for(int i=0;i<m;i++)
{
int id,num;
scanf("%d %d",&id,&num);
for(int j=0;j<num;j++)
{
int input;
scanf("%d",&input);
tree[id].child.push_back(input);
}
}
layerorder(1);
sort(save,save+n+1,cmp);
cout<<save[0].num<<" "<<save[0].layer<<endl;
system("pause");
return 0;
}
1106 Lowest Price in Supply Chain
#include <iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<stack>
//1094 The Largest Generation
/*
*/
using namespace std;
const int inf=0x3fffffff;
const int maxn=100010;
struct node
{
vector<int> child;
double price;
}tree[maxn];
int n;
double price,c;
double mina=inf;
int num=0;
void layerorder(int s)
{
queue<int> q;
q.push(s);
tree[s].price=price;
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=0;i<tree[top].child.size();i++)
{
int v=tree[top].child[i];
tree[v].price=tree[top].price+tree[top].price*c*0.01;
q.push(v);
}
}
}
void preorder(int s)
{
if(tree[s].child.size()==0)
{
if(tree[s].price<mina)
{
mina=tree[s].price;
num=1;
}
else if(tree[s].price==mina)
{
num++;
}
}
for(int i=0;i<tree[s].child.size();i++)
{
preorder(tree[s].child[i]);
}
}
int main()
{
cin>>n>>price>>c;
for(int i=0;i<n;i++)
{
int num;
scanf("%d",&num);
for(int j=0;j<num;j++)
{
int input;
scanf("%d",&input);
tree[i].child.push_back(input);
}
}
layerorder(0);
preorder(0);
printf("%0.4lf %d",mina,num);
system("pause");
return 0;
}
1004 Counting Leaves
#include <iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<stack>
//Counting Leaves
/*
*/
using namespace std;
const int inf=0x3fffffff;
const int maxn=100010;
struct Node
{
vector<int> child;
int id;
int layer;
}tree[maxn];
int n,m;
int anw[maxn]={0};
int max_layer=-5;
void layerorder(int root)
{
int num=0;
queue<int> q;
q.push(root);
tree[root].layer=0;
while(!q.empty())
{
int top=q.front();
q.pop();
if(tree[top].child.size()==0)
{
anw[tree[top].layer]++;
}
if(tree[top].layer>max_layer)
max_layer=tree[top].layer;
for(int i=0;i<tree[top].child.size();i++)
{
tree[tree[top].child[i]].layer=tree[top].layer+1;
q.push(tree[top].child[i]);
}
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<m;i++)
{
int id,num;
scanf("%d %d",&id,&num);
for(int j=0;j<num;j++)
{
int input;
scanf("%d",&input);
tree[id].child.push_back(input);
}
}
layerorder(1);
for(int i=0;i<=max_layer;i++)
{
if(i==0)
{
printf("%d",anw[i]);
}
else
{
printf(" %d",anw[i]);
}
}
system("pause");
return 0;
}
1053 Path of Equal Weight
树的每条路径的遍历存在固定模板,实际上就是深度优先算法。
#include <iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<stack>
//1053 Path of Equal Weight
/*
*/
using namespace std;
typedef long long ll;
const int inf=0x3fffffff;
const int maxn=100010;
struct node
{
vector<int> child;
int id;
int weight;
}tree[maxn];
int n,m;
int wei;
vector<int> temp;
vector<vector<int> > anw;
void DFS(int root,int sum)
{
if(sum>wei)
return;
if(tree[root].child.size()==0)
{
if(sum==wei)
{
temp.push_back(tree[root].weight);
anw.push_back(temp);
temp.pop_back();
}
return;
}
temp.push_back(tree[root].weight);
for(int i=0;i<tree[root].child.size();i++)
{
DFS(tree[root].child[i],sum+tree[tree[root].child[i]].weight);
}
temp.pop_back();
}
bool cmp(vector<int> a,vector<int> b)
{
int length_a=a.size();
int length_b=b.size();
for(int i=0;i<length_a&&i<length_b;i++)
{
if(a[i]==b[i])
continue;
else
{
return a[i]>b[i];
}
}
return a[0]>b[0];
}
int main()
{
cin>>n>>m>>wei;
for(int i=0;i<n;i++)
{
cin>>tree[i].weight;
}
for(int i=0;i<m;i++)
{
int id,num;
scanf("%d %d",&id,&num);
for(int j=0;j<num;j++)
{
int input;
scanf("%d",&input);
tree[id].child.push_back(input);
}
}
DFS(0,tree[0].weight);
sort(anw.begin(),anw.end(),cmp);
for(int i=0;i<anw.size();i++)
{
for(int j=0;j<anw[i].size();j++)
{
if(j==0)
{
printf("%d",anw[i][j]);
}
else
printf(" %d",anw[i][j]);
}
printf("\n");
}
system("pause");
return 0;
}