这个是我们数据结构的一个实验题。我这边时间超紧张,然后就自己写了一个这样的:
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int k, counter = 0;
int i = 0;
char s[MAX];
typedef struct node{
char data;
struct node *lchild, *rchild;
}nod;
void prun(nod *tree){
if(tree){
printf("%c", tree->data);
s[i++] = tree->data;
prun(tree->lchild);
prun(tree->rchild);
}
}
nod *build(){
char x;
nod *p;
scanf("%c", &x);
printf("%c", x);
if(x != ‘.’){
p = (nod *)malloc(sizeof(nod));
p->data = x;
p->lchild = build();
p->rchild = build();
}
else
p = NULL;
return p;
}
int main(void)
{
nod *Tree;
printf("n****************************************n");
printf("输入二叉树:");
Tree = build();
putchar(‘n’);
if(!Tree)
printf("这是一颗空的二叉树");
else{
printf("此二叉树前序遍历的结果为: ");
prun(Tree);
}
printf("n输入K的值:");
scanf("%d", &k);
printf("n前序遍历中第 K 个结点为:%c", s[k - 1]);
putchar(‘n’);
return 0;
}
我自己感觉出来的问题:
不应该有那个数组。这样太浪费空间
可是我也一时想不出来怎么做好? 哪位大侠提示一个?