第26800题 单选题
求给定树广度优先遍历代码的时间复杂度(n为节点数)

令n是树的节点数目,下列代码实现了树的广度优先遍历,其时间复杂度是()。

from collections import deque

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

def bfs(root):
    if not root:
        return

    q = deque()
    q.append(root)

    while q:
        node = q.popleft()
        print(node.val, end=" ")

        if node.left:
            q.append(node.left)
        if node.right:
            q.append(node.right)
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析