AcWing 1261. 查找二叉树
原题链接
简单
作者:
YAX_AC
,
2024-11-28 15:36:22
,
所有人可见
,
阅读 7
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int data;
int left, right;
} tree[102];
int n, t, cnt,res;
void inorder(int i)// i 是代表节点编号。
{
//左子树
if(tree[i].left!=0) inorder(tree[i].left);
//根
cnt++;
if(tree[i].data == t)
{
res = cnt;
}
//右子树
if(tree[i].right!=0) inorder(tree[i].right);
}
int main()
{
cin>>n>>t;
for (int i = 1; i <= n; i ++ )
{
cin>>tree[i].data>>tree[i].left>>tree[i].right;
}
inorder(1);
cout << res;
return 0;
}