C语言二叉树的节点查找问题（递归方法） 5C

`````` struct node *search(struct node *n,int v){//查找
struct node *p;
p=n;

if(p->value==v){//找到
return p;

}
else if(v<p->value){//左边部分查找
if(p->left==NULL)
return NULL;//未找到
else{
p=p->left;

search(p,v);
}
}
else{//v>p->value,右边部分查找
if(p->right==NULL)
return NULL;//未找到
else{
p=p->right;
search(p,v);

}
}
｝
``````

main方法中：point=search(root,20);

4个回答

``````
status search(ptree t,ptree &p,char ch)
{
if(t == NULL)
return !OK;

if (t->data == ch) {
p = t;
return OK;
}

if(!search(t->lchild,p,ch))
search(t->rchild, p, ch);
}

``````

return search(p, v);

search(p,v);

status search(ptree t,ptree &p,char ch)
{
if(t == NULL)
return !OK;

``````if (t->data == ch) {
p = t;
return OK;
}

if(!search(t->lchild,p,ch))
search(t->rchild, p, ch);
``````

}

struct node *search(struct node **n,int v)

struct node **p;

`````` int
Search_data(TreeNode *t,TreeNode **p,TreeNode **q, elemtype x)
{
int flag=0;
*p=NULL;
*q=t;

while(*q)
{
if (x>(*q)->data)
{
*p=*q;
*q=(*q)->Rchild;
}
else{
if (x<(*q)->data)
{
*p=*q;
*q=(*q)->Lchild;
}
else{
flag=1;
break;
}
}
}
return flag;
}
``````