K - 数据结构实验之二叉树的建立与遍历 | SDUT OnlineJudge
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
char s[110];
int i,sum;
struct tree
{
char data;
tree *lc,*rc;
}*T;
tree *creat()///按照先序遍历创建一颗二叉树;
{
tree *t;
if(s[i++]==',')
t=NULL;
else
{
t=new tree;
t->data=s[i-1];
t->lc=creat();
t->rc=creat();
}
return t;
}
void inorder(tree *T)///通过递归输出中序遍历的结果
{
if(T!=NULL)
{
inorder(T->lc);
cout<<T->data;
inorder(T->rc);
}
}
void postorder(tree *T)///通过递归输出后序遍历的结果;
{
if(T!=NULL)
{
postorder(T->lc);
postorder(T->rc);
cout<<T->data;
}
}
void coutleaf(tree *T)
{
if(T)///判断当前节点是否为空;
{
if((!T->lc)&&(!T->rc))
sum++;
coutleaf(T->lc);
coutleaf(T->rc);
}
}
int depth(tree *T)
{
int l,r;
if(!T)///如果为空则输出0;
return 0;
else
{
l=depth(T->lc);
r=depth(T->rc);
return l>=r?l+1:r+1;///+1是为了加上根节点;
}
}
int main()
{
while(~scanf("%s",s))
{
sum=0;///用来统计叶子数;
i=0;
T=creat();
inorder(T);
cout<<endl;
postorder(T);
cout<<endl;
coutleaf(T);
cout<<sum<<endl;
cout<<depth(T);
}
}
数据结构实验之求二叉树后序遍历和层次遍历 | SDUT OnlineJudge
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int inf = 0x3f3f3f3f;
struct tree
{
char l;
struct tree *left,*right;
};
struct tree *build(char *p,char *s,int n)
{
struct tree *tail;
int i;
if(n <= 0)return NULL;
tail = (struct tree *)malloc(sizeof(struct tree));
for(i = 0;i <= n-1;i ++)
{
if(p[0] == s[i])
break;
}
tail ->l = p[0];
tail -> left = build (p+1,s,i);
tail -> right = build (p+i+1,s+i+1,n-i-1);
return tail;
}
void downshow(struct tree *k)
{
if(k)
{
downshow(k -> left);
downshow(k -> right);
printf("%c",k -> l);
}
}
void stepshow(struct tree *k,char *p,int n)
{
int i,j,start,end,num;
struct tree *point[1000];
start = 0;
end = 0;
num = 0;
point[0] = k;
while(start < n)
{
j = 1;
for(i = start;i <= end;i ++)
{
p[i] = point[i] -> l;
if(point[i] -> left )
{
point[end + j] = point[i] -> left;
j ++;
}
if(point[i] -> right )
{
point[end + j] = point[i] ->right;
j ++;
}
}
start = end + 1;
end = end + j - 1;
}
p[n]='\0';
}
int main()
{
char str1[1000],str2[1000],str3[1000];
int t,l;
struct tree *head;
scanf("%d",&t);
while(t--)
{
memset(str3,0,sizeof(str3));
scanf("%s%s",str1,str2);
l = strlen(str1);
head = build(str1,str2,l);
downshow(head);
printf("\n");
stepshow(head,str3,l);
printf("%s\n",str3);
}
return 0;
}
#include<bits/stdc++.h>
#include<cstdlib>
using namespace std;
const int N=1e6+5;
const int inf = 0x3f3f3f3f;
struct node
{
char data;
struct node *l,*r;
};
struct node *buildtree(int n,char *mid,char *pre)
{
struct node *root;
int i;
if(n==0)
return NULL;
root=(struct node *)malloc(sizeof(struct node));
root->data=pre[n-1];
printf("%c",root->data);
for(i=0;i<n;i++)
{
if(mid[i]==pre[n-1])
break;
}
root->l=buildtree(i,mid,pre);
root->r=buildtree(n-i-1,mid+i+1,pre+i);
return root;
};
int main()
{
int n,len;
char mid[100],pre[100];
scanf("%d",&n);
while(n--)
{
memset(mid,0,sizeof(mid));
memset(pre,0,sizeof(pre));
scanf("%s",mid);
scanf("%s",pre);
len=strlen(mid);
buildtree(len,mid,pre);
printf("\n");
}
return 0;
}
小球drop
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main(){
int d,i;
int k=1;
cin>>d>>i;
for(int j=0;j<d-1;j++){//走d次
//完全二叉树的其中一个结点为n,则它的左孩子为2*n,右孩子为2*n+1;
if(i%2==0){//判断走左子树还是右子树
k=k*2+1;
i/=2;
}
else{
k*=2;
i=(i+1)/2;
}
}
cout<<k<<endl;
return 0;
}