根据先序遍历,空结点用#表示,创建二叉树
使用递归的方法
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct BINARYNODE
{
char ch;
struct BINARYNODE* lchild;
struct BINARYNODE* rchild;
}BinaryNode;
void Recursion(BinaryNode* root)
{
if (root == NULL)
return;
printf("%c", root->ch);
Recursion(root->lchild);
Recursion(root->rchild);
}
BinaryNode* CreateBinaryTree()
{
fflush(stdin);
char ch;
scanf("%c", &ch);
BinaryNode* node;
if (ch == '#')
{
node = NULL;
}
else
{
node = (BinaryNode*)malloc(sizeof(BinaryNode));
node->ch = ch;
node->lchild = CreateBinaryTree(); // 递归创建左子数
node->rchild = CreateBinaryTree(); // 递归创建右子数
}
return node;
}
int main()
{
//CresteBinaryTree();
BinaryNode* root;
root = CreateBinaryTree();
Recursion(root);
system("pause");
return 0;
}