#10396. szjy_6_树_016
szjy_6_树_016
szjy_6_树_016
函数 find 中的 cnt 记录本次查找访问的结点数。程序输出是( )。
#include <iostream>
using namespace std;
struct Node {
int value;
Node* left = nullptr;
Node* right = nullptr;
};
bool find(Node* root, int x, int& cnt) {
if (root == nullptr) return false;
++cnt;
if (root->value == x) return true;
if (x < root->value) return find(root->left, x, cnt);
return find(root->right, x, cnt);
}
int main() {
Node a{2}, b{4}, c{6}, d{8}, e{10};
a.right = &b; b.right = &c; c.right = &d; d.right = &e;
int cnt = 0;
find(&a, 10, cnt);
cout << cnt;
}
{{ select(1) }}
- 2
- 3
- 4
- 5