A28380. 以下代码实现了二叉树的深度优先搜索(DFS),并统计叶子结点的数量,则横线上应填写( )。int countLeafNodes(TreeNode* root) { if (root == nullptr) return 0; stack<TreeNode*> s; s.push(root); int count = 0; while (!s.empty()) { TreeNode* node =…
单选题
困难
知识点
题目描述
以下代码实现了二叉树的深度优先搜索(DFS),并统计叶子结点的数量,则横线上应填写( )。
int countLeafNodes(TreeNode* root) {
if (root == nullptr) return 0;
stack<TreeNode*> s;
s.push(root);
int count = 0;
while (!s.empty()) {
TreeNode* node = s.top();
s.pop();
if (node->left == nullptr && node->right == nullptr) {
count++;
}
if (node->right) s.push(node->right);
———————————————————————— // 在此处填入代码
}
return count;
}选项(单选)
答案解析
详细答案解析为会员权益,按每日次数查看。
开通 / 升级会员
上一题
下一题