#10410. szjy_6_树_030

szjy_6_树_030

szjy_6_树_030

阅读代码,程序输出是( )。

#include <iostream>
using namespace std;

struct Node {
int value;
Node* left = nullptr;
Node* right = nullptr;
};

int countTwo(Node* root) {
if (root == nullptr) return 0;
int now = (root->left != nullptr && root->right != nullptr);
return now + countTwo(root->left) + countTwo(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;  b.right = &e;
c.right = &f;
cout << countTwo(&a);
}

{{ select(1) }}

  • 1
  • 2
  • 3
  • 4