第31732题 单选题
已知Python中二叉树节点类的定义如下,下列哪段代码是二叉树递归前序遍历的正确实现?
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
A
def preorder(root):
    if not root:
        return
    print(root.val)
    preorder(root.left)
    preorder(root.right)
B
def preorder(root):
    if not root:
        return
    preorder(root.left)
    print(root.val)
    preorder(root.right)
C
def preorder(root):
    if not root:
        return
    preorder(root.left)
    preorder(root.right)
    print(root.val)
D
def preorder(root):
    print(root.val)
    preorder(root.left)
    preorder(root.right)
程序运行统计
暂无判题统计
提交0次 正确率0.00%
答案解析