#10382. szjy_6_树_002
szjy_6_树_002
szjy_6_树_002
阅读下面的 C++ 代码,程序输出是( )。
#include <iostream>
using namespace std;
struct Node {
int value;
Node* left = nullptr;
Node* right = nullptr;
};
int leafCount(Node* root) {
if (root == nullptr) return 0;
if (root->left == nullptr && root->right == nullptr) return 1;
return leafCount(root->left) + leafCount(root->right);
}
int main() {
Node a{1}, b{2}, c{3}, d{4}, e{5}, f{6};
a.left = &b; a.right = &c;
b.left = &d;
c.left = &e; c.right = &f;
cout << leafCount(&a);
}
{{ select(1) }}
- 2
- 3
- 4
- 6