信息学竞赛入门了
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
int n, x, w, l, r;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {};
TreeNode(int _val) : val(_val), left(nullptr), right(nullptr) {};
TreeNode(int _val, TreeNode* _left, TreeNode* _right) : val(_val), left(nullptr), right(nullptr) {};
~TreeNode() {}
};
TreeNode* buildTree(vector<tuple<int, int, int>>& data, int idx) {
// recursion exit condition (base case)
if (!idx) return nullptr;
const int w = get<0>(data[idx]);
const int l = get<1>(data[idx]);
const int r = get<2>(data[idx]);
auto root = new TreeNode(w);
root->left = buildTree(data, l);
root->right = buildTree(data, r);
return root;
}
void inOrder(TreeNode* root, vector<int>& vals) {
if (!root) return;
inOrder(root->left, vals);
vals.emplace_back(root->val);
inOrder(root->right, vals);
}
int main(void) {
scanf("%d %d", &n, &x);
// tuple(weight, left, right) 三元组
vector<tuple<int, int, int>> data(n + 1);
for (int i = 1; i <= n; ++i) {
scanf("%d %d %d", &w, &l, &r);
data[i] = {w, l, r };
}
vector<int> vals;
auto root = buildTree(data, 1);
inOrder(root, vals);
int ans = 0;
for (int i = 0; i < vals.size(); ++i)
if (vals[i] == x) { ans = i + 1; break; }
printf("%d\n", ans);
}