資料結構›Ch5 樹狀結構第 26 題/共 48 題
26. Binary Tree Traversal、Reconstruction、Binary Search Tree
#DS-05-026中Binary Tree TraversalReconstructionBinary Search Tree
題組題幹(本題:(A)(B)(C),共 2 小題)點擊展開
There are 5 questions. The following contains two functions for traversing a binary tree:
struct Node
{
char key;
Node * left, * right;
};
void trv1(Node* root)
{
std::queue<Node*> Q;
Q.push(root);
while (!Q.empty()) {
Node* cur = Q.front();
Q.pop();
printf("%c", cur->key);
if (cur->left) Q.push(cur->left);
if (cur->right) Q.push(cur->right);
}
}
void trv2(Node* root)
{
std::stack<Node*> Q;
Q.push(root);
while (!Q.empty()) {
Node* cur = Q.top();
Q.pop();
printf("%c", cur->key);
if (cur->right) Q.push(cur->right);
if (cur->left) Q.push(cur->left);
}
}
(A) [3%] Use the following two traversal sequences (each character is the key of a node) to reconstruct the binary tree. Note: There are more than one possible trees, and you only need to show one of them.
trv1: OPMRTECUS
trv2: OPRTCSUME
(B) [3%] Start with an empty binary search tree and insert the characters in "COMPUTERS" into it. List the resulting preorder traversal sequence.
(C) [2%] Among preorder, postorder, inorder, and level-order traversals, which one(s) can uniquely identify a binary search tree by itself?
📄 交大113
▤完整推導請見《WH 資工筆記 · 資料結構》Ch5 樹狀結構