我是靠谱客的博主 伶俐雪碧,这篇文章主要介绍根据先序,中序,求后序遍历#include #include #include using namespace std; struct Node...,现在分享给大家,希望可以做个参考。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

struct Node
{
char data;
Node *lchild,*rchild;
};

Node* createtree(string pre,string in)
{
Node *root=NULL;
if(pre.length()>0)
{
root=new Node;
root->data=pre[0];
int index=in.find(root->data);
root->lchild=createtree(pre.substr(1,index),in.substr(0,index));
root->rchild=createtree(pre.substr(index+1),in.substr(index+1));
}
return root;
}

void posorder(Node *root)
{
if(root!=NULL)
{
posorder(root->lchild);
posorder(root->rchild);
cout<<root->data;
}
}

int main()
{
string pre_str,in_str;
Node *root;
printf("请输入二叉树的先序序列换行后输入中序序列n");
while(cin>>pre_str>>in_str)
{
root=createtree(pre_str,in_str);
posorder(root);
cout<<endl;
}
return 0;
}

转载于:https://www.cnblogs.com/-xss/p/6011928.html

最后

以上就是伶俐雪碧最近收集整理的关于根据先序,中序,求后序遍历#include #include #include using namespace std; struct Node...的全部内容,更多相关根据先序,中序,求后序遍历#include内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部