测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A67609. 以下代码实现了二叉树的深度优先搜索(DFS),并统计叶子结点的数量,则横线上应填写( )。1 int countLeafNodes(TreeNode* root) {

单选题

题目描述

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

1 int countLeafNodes(TreeNode* root) { 
2  if (root == nullptr) return 0; 
3
4  stack<TreeNode*> s; 
5  s.push(root); 
6  int count = 0; 
7  while (!s.empty()) { 
8   TreeNode* node = s.top(); 
9   s.pop(); 
10
11   if (node->left == nullptr && node->right == nullptr) { 
12    count++; 
13   } 
14 
15   if (node->right) s.push(node->right); 
16   ———————————————————————— // 在此处填入代码 
17  } 
18  return count; 
19 }


选项(单选)