#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); // IO 優化 int num, arr[1001]; short N, count, left[1001], right[1001]; while (cin >> N) { stack<short> s; count = left[0] = right[0] = 0; cin >> arr[count++]; N--; // build the root node first while (N--) { cin >> num; short i = 0; while (true) { if (arr[i] < num) { if (right[i]) i = right[i]; else { right[i] = count++; i = right[i]; arr[i] = num; left[i] = right[i] = 0; break; } } else if (num < arr[i]) { if (left[i]) i = left[i]; else { left[i] = count++; i = left[i]; arr[i] = num; left[i] = right[i] = 0; break; } } else assert(false); } } s.push(0); while (!s.empty()) { short i = s.top(); s.pop(); if (right[i]) s.push(right[i]); if (left[i]) s.push(left[i]); cout << arr[i] << " "; } cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); // IO 優化 int num, arr[1001]; short N, count, left[1001], right[1001]; while (cin >> N) { stack<short> s; count = left[0] = right[0] = 0; cin >> arr[count++]; N--; // build the root node first while (N--) { cin >> num; short i = 0; while (true) { if (arr[i] < num) { if (right[i]) i = right[i]; else { right[i] = count++; i = right[i]; arr[i] = num; left[i] = right[i] = 0; break; } } else if (num < arr[i]) { if (left[i]) i = left[i]; else { left[i] = count++; i = left[i]; arr[i] = num; left[i] = right[i] = 0; break; } } else assert(false); } } s.push(0); while (!s.empty()) { short i = s.top(); s.pop(); if (right[i]) s.push(right[i]); if (left[i]) s.push(left[i]); cout << arr[i] << " "; } cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); // IO 優化 int num, arr[1001]; short N, count, left[1001], right[1001]; while (cin >> N) { stack s; count = left[0] = right[0] = 0; cin >> arr[count++]; N--; // build the root node first while (N--) { cin >> num; short i = 0; while (true) { if (arr[i] < num) { if (right[i]) i = right[i]; else { right[i] = count++; i = right[i]; arr[i] = num; left[i] = right[i] = 0; break; } } else if (num < arr[i]) { if (left[i]) i = left[i]; else { left[i] = count++; i = left[i]; arr[i] = num; left[i] = right[i] = 0; break; } } else assert(false); } } s.push(0); while (!s.empty()) { short i = s.top(); s.pop(); if (right[i]) s.push(right[i]); if (left[i]) s.push(left[i]); cout << arr[i] << " "; } cout << "\n"; } return 0; }
咦咦 為什麼總是貼不好程式碼呢???
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); // IO 優化 int num, arr[1001]; short N, count, left[1001], right[1001]; while (cin >> N) { stack<short> s; count = left[0] = right[0] = 0; cin >> arr[count++]; N--; // build the root node first while (N--) { cin >> num; short i = 0; while (true) { if (arr[i] < num) { if (right[i]) i = right[i]; else { right[i] = count++; i = right[i]; arr[i] = num; left[i] = right[i] = 0; break; } } else if (num < arr[i]) { if (left[i]) i = left[i]; else { left[i] = count++; i = left[i]; arr[i] = num; left[i] = right[i] = 0; break; } } else assert(false); } } s.push(0); while (!s.empty()) { short i = s.top(); s.pop(); if (right[i]) s.push(right[i]); if (left[i]) s.push(left[i]); cout << arr[i] << " "; } cout << "\n"; } return 0; }
你的複製貼上發生什麼事了XD