编写递归算法,在二叉树中求位于先序序列中第k个位置的结点的值(请严格按照样例进行输入输出,先输入二叉树树,再输入k的值)。
样例如下:
输入:ABD..EH...CF.I..G..
5
输出:H
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define Error 2
typedef char Telemtype;
typedef int status;
typedef struct QiNode {
Telemtype date;
QiNode* lchild, * rchild;
}QiNode,*QiTree;
status creat(QiTree& T) {
char ch;
scanf_s("%c",&ch);
if (ch == '.')
T = NULL;
else {
T = (QiNode*)malloc(sizeof(QiNode));
T->date = ch;
creat(T->lchild);
creat(T->rchild);
}
return OK;
}
status perOrder(QiTree T,int n) {
if (!T)
return Error;
static int i = 1;
if (i == n)
printf("%c", T->date);
i++;
perOrder(T->lchild,n);
perOrder(T->rchild,n);
return OK;
}
int main() {
int n;
QiTree T;
creat(T);
scanf_s("%d", &n);
perOrder(T,n);
return 0;
}