二叉树栈实现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;
}
正在加载题目数据…
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析