#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct BiTNode{
char data;
struct BiTNode *Lchild, *Rchild;
}BiTNode,*BiTree;
// 6_41
bool Find_K(BiTree B1,int* i, int k){
if(B1 == NULL) return false;
(*i)++;
if(k == *i){
printf("%c\n", B1->data);
return true;
}
if(Find_K(B1->Lchild,i,k) || Find_K(B1->Rchild,i,k))
return true;
return false;
}
// create Binary Tree
BiTree CreateBiTree(BiTree T){
char ch;
scanf("%c", &ch);
if(ch == '#') T = NULL;
else{
if(!(T = (BiTNode*)malloc(sizeof(BiTNode)))) return false;
T->data = ch;
T->Lchild = CreateBiTree(T->Lchild);
T->Rchild = CreateBiTree(T->Rchild);
}
return T;
}
int main()
{
BiTree T1 = NULL,B1;
int k;
B1 = CreateBiTree(T1);
printf("success !\n");
int a=0;
scanf("%d",&k);
if(Find_K(B1,&a,k)){
printf("find K!");
}else{
printf("sorry, can't find K!");
}
return 0;
}
编写递归算法,在二叉树中求位于先序序列中第k个位置的结点的值
于 2021-12-13 12:18:38 首次发布