TreeNode *BinaryTreeFormorderings(char *Preorder, char *Inorder, int len)
{
if(len == 0)
return NULL;
TreeNode *node=new TreeNode;
node->val=*Preorder;
int rootIndex=0;
while(rootIndex<len)
{
if(Inorder[rootIndex] == *Preorder)
break;
++rootIndex;
}
node->left = BinaryTreeFormorderings(Preorder+1, Inorder, rootIndex);
node->right = BinaryTreeFormorderings(Preorder+rootIndex+1,Inorder+rootIndex+1,len-(rootIndex+1));
cout<<node->val;
return node;
}
注:上述代码,其实是在遍历的过程中建立了一个二叉树并返回,这是最全的。如果只是要个后续遍历的序列,可以在输出哪里以一个vector来存放序列就可以了,没必要建立二叉树的。如果只是要输出后续遍历,则在最后哪里输出就可以了,也没必要保存这个vector了。
2、已知后和中序,求前序遍历:类似上述求后序的情况
TreeNode *BinaryTreePostorderings(char *Postorder, char *Inorder, int len)
{
if (len == 0)
return NULL;
TreeNode *node = new TreeNode;
node->val = *(Postorder + len - 1);
cout << node->val;
int rootIndex = 0;
while (rootIndex<len)
{
if (Inorder[rootIndex] == *(Postorder + len - 1))
break;
++rootIndex;
}
node->left = BinaryTreePostorderings(Postorder, Inorder, rootIndex);
node->right = BinaryTreePostorderings(Postorder + rootIndex, Inorder + rootIndex + 1, len - (rootIndex + 1));
return node;
}
注:上述代码,与前文情况一致,根据具体情况需要来完成前序的遍历。
又写了一次遍历方式,用vector存放数据,起点终点,函数参数传入:
void BinaryTree::PreIn_to_post(std::vector<int> &pre, int first, int end, std::vector<int> &in, int first2, int end2, std::vector<int> &post) { if (pre.size() == 0 || in.size() == 0 || first<0 || end>pre.size()-1 || first2<0 || end2>in.size()-1 ||first>end || first2>end2 || end-first != end2-first2) return; if (first == end) { post.push_back(pre[first]); return; } int index2 = first2; while (index2 <= end2 && in[index2] != pre[first]) ++index2; PreIn_to_post(pre, first + 1, first+index2-first2, in, first2, index2-1, post); PreIn_to_post(pre, first+ 1+index2 -first2, end, in, index2 + 1, end2, post); post.push_back(pre[first]); } void BinaryTree::PostIn_to_pre(std::vector<int> &post, int first, int end, std::vector<int> &in, int first2, int end2, std::vector<int> &pre) { if (post.size() == 0 || in.size() == 0 || first<0 || end>post.size() - 1 || first2<0 || end2>in.size() - 1 || first > end || first2 > end2 || end - first != end2 - first2) return; if (first == end) { pre.push_back(post[end]); return; } int index2 = first2; while (index2 <= end2&&in[index2] != post[end]) ++index2; pre.push_back(post[end]); PostIn_to_pre(post, first, first + (index2 - first2)-1, in, first2, index2 - 1, pre); PostIn_to_pre(post, first + index2 - first2, end - 1, in, index2 + 1, end2,pre); }
3、已知前序后序遍历,求中序遍历:
这种情况并不能完全的求出所有情况的二叉树中序遍历。因为由前序和后序并不能完全确定一颗树。但是,如果已知是满二叉树或者完全二叉树的话,我想是可以的。