# 二叉排序树的实现和查找

#### 输入

关键字个数n；

#### 输出

查找成功输出比较的次数，否则输出-1。

#### 样例输入

12
25 18 46 2 53 39 32 4 74 67 60 11
74


#### 样例输出

4
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int flag;
struct btree
{
int date;
btree *left, *right;
};

{
root  = new btree;
cin  >> root->date;
root->left  = root->right  = NULL;
}

void creatbtree(btree *& root, int insert)
{
if (root  == NULL)
{
root  = new btree;
root->date  = insert;
root->left  = root->right  = NULL;
return;
}
if (root->date < insert)
{
creatbtree(root->right, insert);
}
else if (root->date>insert)
{
creatbtree(root->left, insert);
}
}

void find(btree *root, int tar, int cnt)
{
if (root  == NULL)
{
return;
}
if (tar  == root->date)
{
cout  << cnt + 1;
flag  = 1;
return;
}
else if (tar < root->date)
{
find(root->left, tar, cnt  + 1);
}
else if (tar > root->date)
{
find(root->right, tar, cnt  + 1);
}
}

int main()
{
int n;
cin  >> n;
btree *root;
int k;
flag  = 0;
int num;
for (int i  = 2; i  <= n; i++)
{
cin  >> num;
creatbtree(root, num);
}

cin  >> k;
find(root, k, 0);
if (!flag)
cout  << "-1";
return 0;
}