许诺 • 12天前
#include <iostream>
#include <vector>
using namespace std;
struct Node {
int value;
int left;
int right;
};
vector tree; int target; int count = 0; int result = 0;
void inorder(int root) {
if (root == 0) {
return;
}
inorder(tree[root].left);
count++;
if (tree[root].value == target) {
result = count;
return;
}
inorder(tree[root].right);
}
int main() {
int n;
cin >> n >> target;
tree.resize(n + 1); // 使用1-based索引
for (int i = 1; i <= n; i++) {
cin >> tree[i].value >> tree[i].left >> tree[i].right;
}
inorder(1);
cout << result << endl;
return 0;
}
评论:
请先登录,才能进行评论