第26832题 单选题
Python二叉树非递归DFS统计叶子节点的横线填空

以下代码实现了二叉树的深度优先搜索(DFS),并统计了叶子节点的数量。横线上应填写( )。

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

def count_leaf_nodes(root):
    if root is None:
        return 0

    stack = []
    stack.append(root)
    count = 0

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