NYOJ 202 红黑树 数组模拟中序遍历
2012-05-09 08:35 javaspring 阅读(311) 评论(0) 编辑 收藏 举报题目其实在迷惑人了,红黑树经过旋转后中序遍历其实是不变的,所以不用管下面的旋转,直接输出中序遍历就可以了。可以用数组模拟实现树的中序遍历。
题目连接:http://acm.nyist.net/JudgeOnline/problem.php?pid=202
ac代码:
#include <iostream> #include <cstdio> #include <string.h> using namespace std; const int N = 15; int x[N],y[N],z[N],leftp[N],rightp[N]; void midorder(int x) { if(x != -1) { midorder(leftp[x]); printf("%d\n",x); midorder(rightp[x]); } } int main() { int numcase; scanf("%d",&numcase); for(int k = 1;k <= numcase;++k) { int n; scanf("%d",&n); for(int i = 0;i < n;++i) { scanf("%d%d%d",&x[i],&y[i],&z[i]); } for(int i = 0;i < n;++i) { leftp[x[i]] = y[i]; rightp[x[i]] = z[i]; } int m,xx,yy; scanf("%d",&m); while(m--) { scanf("%d%d",&xx,&yy); } midorder(0); printf("\n"); } return 0; }