#24882: 用陣列模擬 linked list,用 stack 實作 preorder traversal


allllllan123456 (God of Computer Science)

學校 : 國立臺灣大學
編號 : 13732
來源 : [140.109.20.138]
最後登入時間 :
2021-07-08 17:41:52
d526. Binary Search Tree (BST) | From: [111.242.236.97] | 發表日期 : 2021-04-03 15:12

#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; }

 
#24883: Re:用陣列模擬 linked list,用 stack 實作 preorder traversal


allllllan123456 (God of Computer Science)

學校 : 國立臺灣大學
編號 : 13732
來源 : [140.109.20.138]
最後登入時間 :
2021-07-08 17:41:52
d526. Binary Search Tree (BST) | From: [111.242.236.97] | 發表日期 : 2021-04-03 15:13

#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; }

 
#24884: Re:用陣列模擬 linked list,用 stack 實作 preorder traversal


allllllan123456 (God of Computer Science)

學校 : 國立臺灣大學
編號 : 13732
來源 : [140.109.20.138]
最後登入時間 :
2021-07-08 17:41:52
d526. Binary Search Tree (BST) | From: [111.242.236.97] | 發表日期 : 2021-04-03 15:14

#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; }


咦咦 為什麼總是貼不好程式碼呢???

 
#25971: Re:用陣列模擬 linked list,用 stack 實作 preorder traversal


vic20050418@gmail.com (Wen Vic)

學校 : 國立臺灣科技大學
編號 : 153262
來源 : [114.136.159.95]
最後登入時間 :
2023-07-29 13:10:41
d526. Binary Search Tree (BST) | From: [114.136.189.236] | 發表日期 : 2021-07-08 19:49

#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

 
ZeroJudge Forum