链接:click here
题意:题目其实很简单,绕一大圈,原来就是叫你输出输出中序遍历,Orz~~~红黑树经过旋转后中序遍历其实是不变的,所以与下面的旋转没有关系~~--- _ --.
思路:直接数组模拟,或用结构体:包含(数据域,左子树,右子树)
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=20;
const int inf =0x3f3f3f3f;
int N,F,D;
int leftt[maxn],rightt[maxn];
struct node
{
int data;
int leftchild,rightchild;
} tree[maxn];
void inorder(int key)
{
if(key!=-1)
{
inorder(tree[key].leftchild);
printf("%d\n",tree[key].data);
inorder(tree[key].rightchild);
}
}
int main()
{
int T,root,ld,rd;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d%d%d",&root,&ld,&rd);
tree[root].data=root;
tree[root].leftchild=ld;
tree[root].rightchild=rd;
// scanf("%d%d%d",&tree[i].data,&tree[i].leftchild,&tree[i].rightchild);
}
int m,ve,mv;
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&ve,&mv);
}
inorder(0);
printf("\n");
}
return 0;
}
原文地址:http://blog.csdn.net/u013050857/article/details/43406911