TopCoder

User's AC Ratio

100.0% (1/1)

Submission's AC Ratio

100.0% (1/1)

Tags

Description

給定一些互異的整數,依照輸入順序插入二元搜尋樹(Binary Search Tree,BST),接著回答遍歷操作。

操作定義如下:

  • 0:輸出 preorder(根、左、右)。
  • 1:輸出 inorder(左、根、右)。
  • 2:輸出 postorder(左、右、根)。

Input Format

第一行有兩個整數 n q

第二行有 n 個互異整數,依序插入 BST。若 n = 0,此行為空行。

接下來有 q 行,每行是一個操作。

Output Format

每個操作輸出一行遍歷結果。元素之間以一個空白分隔,不要輸出行尾空白。若樹為空,輸出空行。

Sample Input 1

7 3
4 2 1 3 6 5 7
0
1
2

Sample Output 1

4 2 1 3 6 5 7
1 2 3 4 5 6 7
1 3 2 5 7 6 4

Hints

  • 0 <= n <= 2000
  • 1 <= q <= 2000
  • 節點值皆為 32-bit signed integer,且互不相同。

Problem Source

tioj/problem/bst-traversal

Subtasks

No. Testdata Range Score
1 0~2 100

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 65536 65536 1
1 1000 65536 65536 1
2 1000 65536 65536 1