已知二叉树的中序遍历和后序遍历,如何求前序遍历
来源:互联网 发布:《鸟哥的linux私房菜》 编辑:程序博客网 时间:2024/05/22 06:05
在已知二叉树的前序遍历和中序遍历的情况下,求出了后序遍历。
那么,对称地,如果已知二叉树的后序遍历和中序遍历,如何求前序遍历呢?
其实思路与上文完全类似。
代码如下:
- // 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;
- }
// 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;}
- 已知二叉树的中序遍历和后序遍历,如何求前序遍历
- 已知二叉树的中序遍历和后序遍历,如何求前序遍历
- 已知二叉树的后序遍历和中序遍历求前序遍历(二叉树)
- 二叉树遍历问题:已知中序遍历和后序遍历结果,求前序遍历结果
- 已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?
- 已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?
- 已知二叉树的中序遍历和后序遍历,求先序遍历
- 已知二叉树的后序遍历和中序遍历重建二叉树(二叉树)
- 已知二叉树的中序遍历和前序遍历,如何求后序遍历
- 已知二叉树的中序遍历和前序遍历,如何求后序遍历
- 已知二叉树的先序遍历和中序遍历,如何求后序遍历
- 已知二叉树的中序遍历和前序遍历,如何求后序遍历
- 已知二叉树的后序/前序遍历和中序遍历,求前序/中序遍历
- 【树】已知二叉树前序和中序遍历求后序遍历,及中序和后序遍历求前序遍历
- 二叉树遍历:已知中序和后序,求前序
- 已知二叉树后序遍历和中序遍历,求前序遍历
- 已知中序遍历和后序遍历,求前序遍历
- 已知后序遍历和中序遍历求前序遍历
- tcp短连接TIME_WAIT问题解决方法大全(3)——tcp_tw_recycle
- CSS中行内元素与块级元素
- 第三十一,swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上?
- 获取表格的绝对位置
- 第十一章 异常处理
- 已知二叉树的中序遍历和后序遍历,如何求前序遍历
- 用Java Swing编写简单的测试小工具界面(源码)
- tcp短连接TIME_WAIT问题解决方法大全(4)——tcp_tw_reuse
- 系统命令跟踪命令工具
- 直方图之calcHist使用 .
- 如运行程序提示:“没有找到libmysql.dll,因此这个应用程序未能启动。重新安装应用程序可能会修复此问题。
- Long也是整数,为什么不能传给SWITCH呢?
- tcp短连接TIME_WAIT问题解决方法大全(5)——tcp_max_tw_buckets
- typedef的用法