#include using namespace std; const int N = 1e6 + 10; //树的结构体+存储数组 struct Node { int id; // 当前结点ID int left; // 左结点ID int right;// 右结点ID } t[N]; int n; /** 测试用例: 7 2 7 3 6 4 5 0 0 0 0 0 0 0 0 */ queue q; int main() { cin >> n; //创建二叉树 build for (int i = 1; i <= n; i++) cin >> t[i].left >> t[i].right, t[i].id = i; //放入第一个 q.push(t[1]); while (!q.empty()) { Node n1 = q.front(); q.pop(); cout << n1.id << " "; if (n1.left > 0) q.push(t[n1.left]); if (n1.right > 0) q.push(t[n1.right]); } return 0; }