#10414. szjy_6_树_034

szjy_6_树_034

szjy_6_树_034

下面用 firstChildnextSibling 表示普通树。程序输出是( )。

#include <iostream>
using namespace std;

struct Node {
char value;
Node* firstChild = nullptr;
Node* nextSibling = nullptr;
};

int childCount(Node* root) {
int ans = 0;
for (Node* p = root->firstChild; p != nullptr; p = p->nextSibling)
++ans;
return ans;
}

int main() {
Node r{'R'}, a{'A'}, b{'B'}, c{'C'};
r.firstChild = &a;
a.nextSibling = &b;
b.nextSibling = &c;
cout << childCount(&r);
}

{{ select(1) }}

  • 2
  • 3
  • 4
  • 1