http://acm.nyist.net/JudgeOnline/problem.php?pid=202
直接输出中序,因为左旋右旋后中序并不改变!!!
开始以为要建树,后来发现不用啊!因为那个输入就是树了,都把每个节点的左孩子右孩子给出来了,那就直接中序遍历之。
#include<cstdio>
struct Node{
int data;
int lchild,rchild;
}tree[11];
void print1(int key){
if(key!=-1){
print1(tree[key].lchild);
printf("%d\n",tree[key].data);
print1(tree[key].rchild);
}
}
int main(){
int T;
int root,ld,rd,num,m;
scanf("%d",&T);
while(T--){
scanf("%d",&num);
for(int i=0;i<num;i++){
scanf("%d%d%d",&root,&ld,&rd);
tree[root].data=root;
tree[root].lchild=ld;
tree[root].rchild=rd;
}
scanf("%d",&m);
while(m--)
scanf("%*d%*d");
print1(0);
printf("\n");
}
}

本文介绍了一种利用中序遍历不变性的方法来解决特定类型的树形结构问题。通过直接输出中序遍历结果避免了复杂的树构建过程,简化了解决方案。文章提供了完整的C语言实现代码。

318

被折叠的 条评论
为什么被折叠?



