已知二叉树的中序遍历和后序遍历,如何求前序遍历
2011-09-24 17:35
459 查看
上文在已知二叉树的前序遍历和中序遍历的情况下,求出了后序遍历。
那么,对称地,如果已知二叉树的后序遍历和中序遍历,如何求前序遍历呢?
其实思路与上文完全类似。
代码如下:
那么,对称地,如果已知二叉树的后序遍历和中序遍历,如何求前序遍历呢?
其实思路与上文完全类似。
代码如下:
// InPost2Pre.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; struct TreeNode { struct TreeNode* left; struct TreeNode* right; char elem; }; TreeNode* BinaryTreeFromOrderings(char* inorder, char* postorder, int length) { if(length == 0) { return NULL; } //We have root then; TreeNode* node = new TreeNode;//Noice that [new] should be written out. node->elem = *(postorder+length-1); cout<<node->elem<<endl; int rootIndex = 0; for(;rootIndex <length; rootIndex++) { if(inorder[rootIndex] == *(postorder+length-1)) break; } //在循环条件而非循环体完成了对变量值的改变。 //Left node->left = BinaryTreeFromOrderings(inorder, postorder, rootIndex); //Right node->right = BinaryTreeFromOrderings(inorder + rootIndex + 1, postorder + rootIndex, length - (rootIndex+1)); //node->right = *(postorder+length-2); return node; } int main(int argc, char* argv[]) { printf("Hello World!\n"); char* in="ADEFGHMZ"; char* po="AEFDHZMG"; BinaryTreeFromOrderings(in, po, 8); printf("\n"); return 0; }
相关文章推荐
- 已知二叉树的中序遍历和前序遍历,如何求后序遍历
- 二叉树系列(1)已知二叉树的中序遍历和前序遍历,如何求后序遍历
- 已知二叉树的中序遍历和前序遍历,如何求后序遍历 .
- 二叉树系列(1)已知二叉树的中序遍历和前序遍历,如何求后序遍历
- 已知二叉树的中序遍历和后序遍历,如何求前序遍历
- 已知二叉树的中序遍历和前序遍历,如何求后序遍历
- 已知二叉树的中序遍历和前序遍历,如何求后序遍历
- 对于已知二叉树的中序遍历和后序遍历如何求二叉树的的前序遍历
- 已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?
- 计算机技术——已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?
- 已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?
- 二叉树的中序遍历和前序遍历,如何求后序遍历
- 已知二叉树的前序遍历中序遍历,求后序遍历
- 已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?
- 二叉树系列:已知二叉树的中序遍历和前序遍历,求后序遍历
- 已知二叉树的前序遍历、中序遍历,求后序遍历
- 已知二叉树的前序遍历、中序遍历或者中序遍历、后序遍历求二叉树结构的算法
- 二叉树的建立,前序遍历,中序遍历,后序遍历
- 二叉树的创建、前序遍历、中序遍历、后序遍历
- 已知前序遍历序列和中序遍历序列,求二叉树的后序遍历