单选题 适中
#17732

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

以下代码实现了二叉树的深度优先搜索(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
未登录也能判分,登录后才会记录
答案与解析
正确答案:A
这道题暂未补解析。