//简单的搜索题 #include<iostream> #include<vector> using namespace std; struct Node { char id; int left; int right; bool root; }node[1001]; void print(int i)//递归打印先序遍历 { if(i == 0) return; cout << node[i].id; print(node[i].left); print(node[i].right); } int main() { int n,index,root_num; //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(cin >> n) { vector<int> v; while(n--) { cin >> index; v.push_back(index); cin >> node[index].id >> node[index].left >> node[index].right; node[index].root = 1; } for(int i = 0;i < v.size();i++) { int l = node[v[i]].left,r = node[v[i]].right; node[l].root = 0; node[r].root = 0; }//将有被指向的结点将root置零,表示不是根节点。则最后没有被置零的为根节点 for(int i = 0;i < v.size();i++) { if(node[v[i]].root == 1) root_num = v[i]; }//找出根节点 print(root_num);//打印 cout << endl; } return 0; }