Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
99716 | modongtao | 前序遍历 | C++ | 解答错误 | 0 | 0 MS | 264 KB | 649 | 2023-08-23 16:15:14 |
#include <bits/stdc++.h> using namespace std; int tree[100005]; int inp[100005]; void buildTree(int cur, int a, int b) { int mid = (a + b) / 2; tree[cur] = inp[mid]; if (mid != a) buildTree(cur * 2, a, mid-1); if (mid != b) buildTree(cur * 2 + 1, mid+1, b); } void dfs(int cur) { if (tree[cur] == 0) return; cout << tree[cur] << " "; cout << "Node " << cur << " L: " << cur * 2 << " R: " << cur * 2 + 1 << endl; dfs(cur * 2); dfs(cur * 2 + 1); } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> inp[i]; } sort(inp + 1, inp + n + 1); buildTree(1, 1, n); dfs(1); return 0; }