#include <bits/stdc++.h>
using namespace std;
typedef struct node {
char key;
struct node *lchild;
struct node *rchild;
}node, *Tree;
void creat(Tree &root) {
char tmpchar;
scanf("%c", &tmpchar);
if(tmpchar == '#')
root = NULL;
else {
root=(Tree)malloc(sizeof(node));
// cout<<"!!!\n";
root->key = tmpchar;
creat(root->lchild);
creat(root->rchild);
}
}
void Visit(Tree root){
if(root->key != '#'){
printf("%c ", root->key);
}
}
void InOrder(Tree root){
if(root != NULL){
//访问左子结点
InOrder(root->lchild);
//访问根节点
Visit(root);
//访问右子结点
InOrder(root->rchild);
}
}
int main() {
Tree root;
creat(root);
InOrder(root);
cout<<"\n";
return 0;
}
// cf#g#ny#h#t##hpg##h#e##oebr####h##r##
二叉树的前序递归建立和中序递归打印
最新推荐文章于 2023-03-03 16:24:30 发布