已知先序遍历和中序遍历求后序遍历——二叉树
2015-05-18 21:49
375 查看
思想数据结构课都讲过,就是从pre下手,因为处理pre是连续的一段段,头上的都是根节点,所以接口函数为:
void solve(tree* root,int low,int high,int rootpos)
root为当前节点,low,high都是相对于mid串而言的;rootpos为根节点在pre串中的位置
思路递归,代码如下:
[cpp] view
plaincopy
#include "iostream"
#include "string"
using namespace std;
struct tree
{
char name;
tree* left;
tree* right;
};
string pre,mid;
//tree* solve(tree* root,int low,int high,int rootpos)
//{
// if (low>high)
// return NULL;
// if(low==high)
// {
// tree *temp=(tree*) malloc(sizeof(tree*));
// temp->left=temp->right=NULL;
// temp->name=mid[low];
// return temp;
// }
// int rpos=-1;
// while (mid[++rpos]!=pre[rootpos]);
// root->name=pre[rootpos];
// root->left=(tree*) malloc(sizeof(tree*));
// root->right=(tree*) malloc(sizeof(tree*));
// root->left=solve(root->left,low,rpos-1,rootpos+1);
// root->right=solve(root->right,rpos+1,high,rpos+1);
// return root;
//}
void solve(tree* root,int low,int high,int rootpos)
{
if(low==high)
{
root->left=root->right=NULL;
root->name=mid[low];
return;
}
int rpos=-1;
while (mid[++rpos]!=pre[rootpos]);
root->name=pre[rootpos];
root->left=root->right=NULL;
if (low<=rpos-1)
{
root->left=(tree*) malloc(sizeof(tree*));
solve(root->left,low,rpos-1,rootpos+1);
}
if(rpos+1<=high)
{
root->right=(tree*) malloc(sizeof(tree*));
solve(root->right,rpos+1,high,rpos+1);
}
}
void TraversalPost(tree* node)
{
if (node)
{
TraversalPost(node->left);
TraversalPost(node->right);
printf("%c ",node->name);
}
}
void main()
{
int i,j;
tree* root=(tree*)malloc(sizeof(tree*));
while (cin>>pre>>mid)
{
root->left=root->right=NULL;
root->name=mid[0];
solve(root,0,mid.length()-1,0);
TraversalPost(root);
cout<<endl;
}
}
其中被注释掉的那部分也能用的,和下面同名函数是同种方法,两种形式。
调用就是root=solve(root,0,mid,length()-1,0);
=======================================================================
Hope you enjoy it !
void solve(tree* root,int low,int high,int rootpos)
root为当前节点,low,high都是相对于mid串而言的;rootpos为根节点在pre串中的位置
思路递归,代码如下:
[cpp] view
plaincopy
#include "iostream"
#include "string"
using namespace std;
struct tree
{
char name;
tree* left;
tree* right;
};
string pre,mid;
//tree* solve(tree* root,int low,int high,int rootpos)
//{
// if (low>high)
// return NULL;
// if(low==high)
// {
// tree *temp=(tree*) malloc(sizeof(tree*));
// temp->left=temp->right=NULL;
// temp->name=mid[low];
// return temp;
// }
// int rpos=-1;
// while (mid[++rpos]!=pre[rootpos]);
// root->name=pre[rootpos];
// root->left=(tree*) malloc(sizeof(tree*));
// root->right=(tree*) malloc(sizeof(tree*));
// root->left=solve(root->left,low,rpos-1,rootpos+1);
// root->right=solve(root->right,rpos+1,high,rpos+1);
// return root;
//}
void solve(tree* root,int low,int high,int rootpos)
{
if(low==high)
{
root->left=root->right=NULL;
root->name=mid[low];
return;
}
int rpos=-1;
while (mid[++rpos]!=pre[rootpos]);
root->name=pre[rootpos];
root->left=root->right=NULL;
if (low<=rpos-1)
{
root->left=(tree*) malloc(sizeof(tree*));
solve(root->left,low,rpos-1,rootpos+1);
}
if(rpos+1<=high)
{
root->right=(tree*) malloc(sizeof(tree*));
solve(root->right,rpos+1,high,rpos+1);
}
}
void TraversalPost(tree* node)
{
if (node)
{
TraversalPost(node->left);
TraversalPost(node->right);
printf("%c ",node->name);
}
}
void main()
{
int i,j;
tree* root=(tree*)malloc(sizeof(tree*));
while (cin>>pre>>mid)
{
root->left=root->right=NULL;
root->name=mid[0];
solve(root,0,mid.length()-1,0);
TraversalPost(root);
cout<<endl;
}
}
其中被注释掉的那部分也能用的,和下面同名函数是同种方法,两种形式。
调用就是root=solve(root,0,mid,length()-1,0);
=======================================================================
Hope you enjoy it !
相关文章推荐
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知二叉树先序遍历,中序遍历创建二叉树并输出后序遍历
- 已知先序遍历和中序遍历,求后序遍历 && 求二叉树中节点的最大距离
- 已知二叉树先序遍历中序遍历求后序遍历
- 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
- 已知一颗二叉树,如果先序遍历的节点顺序是:ADCEFGHB,中序遍历是,CDFEGHAB,则后序遍历的结果为
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知先序遍历和中序遍历求后序遍历——二叉树
- 已知二叉树的先序遍历(preorder)和中序遍历(inorder) 或 中序和后序(postorder),还原该二叉树
- 算法:二叉树的先序遍历、中序遍历、后序遍历(递归及非递归方式)的java代码实现
- 已知二叉树前序,中序遍历,求后序遍历,java实现
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- 二叉树先序遍历,中序遍历和后序遍历的非递归实现
- 对于已知二叉树的中序遍历和后序遍历如何求二叉树的的前序遍历
- hdu1710 已知二叉树的前序遍历和中序遍历,还原二叉树,求出后序、层次遍历
- 二叉树系列(1)已知二叉树的中序遍历和前序遍历,如何求后序遍历