阅读以下广度优先搜索的代码:
void bfs(TreeNode* root) {
if (root == NULL) {
return;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* current = q.front();
q.pop();
cout << current->val << " ";
if (current->left) {
q.push(current->left);
}
if (current->right) {
q.push(current->right);
}
}
}
使用以上算法遍历以下这棵树,可能的输出是( )。
1
/ \
2 3
/ \ \
8 9 6
/ \ \
4 5 7
/ \
10 11
(树的结构:1 的左孩子为 2、右孩子为 3;2 的左孩子为 8、右孩子为 9;3 的右孩子为 6;9 的左孩子为 4、右孩子为 5;6 的右孩子为 7;7 的左孩子为 10、右孩子为 11。)