以下C++代码实现二叉树深度优先搜索(DFS)统计叶子结点数量,横线处应填写的内容是?

ap298744 单选题 共 个子题
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;
}
A

if (node->left) s.push(node->left);

B

if (node->left) s.pop(node->left);

C

if (node->left) s.front(node->left);

D

if (node->left) s.push(node->right);

正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧