二叉树

二叉树的遍历分为以下三种:

先序遍历:遍历顺序规则为【根左右】

中序遍历:遍历顺序规则为【左根右】

后序遍历:遍历顺序规则为【左右根】


先序遍历:1245736

中序遍历:4275136

后续遍历:4752631



Problem 1261 二叉树的遍历(中序遍历)………………数构

Accepted: 1970    Total Submit: 2327
Time Limit: 1000ms    Memony Limit: 32768KB

Description

二叉树的存储可以用顺序存储和链式存储来完成。现在给你顺序存储的二叉树,请你转化为链式存储,然后按中序遍历的方式输出。

Input

输入有若干种情况,每种情况一行,每行是一个按顺序存储的二叉树。如果结点处空用半角的‘.’代替。

Ouput

每个案例输出一行,按结点的中序遍历输出。

Sample Input

ABCDEFGHIJKL
A.B...C.......D
ABCD.EF.G
T

Sample Output

HDIBJEKALFCG
ABCD
DGBAECF
T

Source

Zmh

代码:

#include"iostream"
#include"algorithm"
using namespace std;
struct node{
	char data;
	node *left,*right;
	node():left(NULL),right(NULL){}
};
char tree[10010];
node* create(int s,int e){
	node *p=NULL;
	if(s>e)
	{
		return p;
	}
	if(tree[s]!='.')
	{
		p=new node();
		p->data=tree[s];
		p->left=create(2*s+1,e);
		p->right=create(2*s+2,e);
	}
	return p;
}
void inOrder(struct node *root){
	if(root != NULL){
		//printf("%c",root->data);//先序遍历
		inOrder(root->left);
		printf("%c",root->data);
		inOrder(root->right);
		//printf("%c",root->data);//后续遍历
	}
}
int main(){
	while(scanf("%s",tree)!=EOF){
		int len = strlen(tree);
		node *root = new node();
		root = create(0,len - 1);
		inOrder(root);
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值