由二叉树的后序和中序求先序
来源:互联网 发布:刷mac os 编辑:程序博客网 时间:2024/06/08 18:03
已知二叉树的后序和中序,求它的先序:
后序:dgebfca
中序:dbegafc
我们可以得到树如下:
a
/ /
b c
/ / /
d e f
/
g
思路: 二叉树的后续遍历为: 左右中,所以根节点肯定在
#include <iostream>
using namespace std;
void GetTreeOrder(char bOrder[], int bs, int be,
char cOrder[], int cs, int ce)
{
//if end < begin, we return.
if(be<bs || ce<cs)
return;
//Get the root data of the current recursion.
char pNewRoot = bOrder[be];
//1. Output the root node by the define of font order.
cout<<pNewRoot;
//if there is only one node, we output it immediately.
if(be==bs || ce==cs)
return;
//here we find the root node in the center order array.
//find i in cOrder[0------cs-------ce------sumEnd]
int i = cs;
for (; i<=ce; i++)
{
if(cOrder[i] == pNewRoot)
break;
}
//here we have found i.
//cOrder: 0------cs----i----ce------sumEnd
//==> left part : [cs----i-1]
// right part: [i+1----ce]
//bOrder: 0------bs----x----be------sumEnd
//&&& x-bs = i-cs; (left)
// be-1-x = ce-i; (right)
//==> left part : [bs----x-1] ==> [bs----bs+i-cs-1]
// right part: [x+1----(be-1)] ==> [be-ce+i----be-1]
//2. Out the left node by the define of font order.
GetTreeOrder(bOrder, bs, bs+i-cs-1, cOrder, cs, i-1);
//3. Output the right node by the define of font order.
GetTreeOrder(bOrder, be-ce+i, be-1, cOrder, i+1, ce);
}
int main(int argc, char* argv[])
{
char BackOrder[] = {'d','g','e','b','f','c','a'};
char CenterOrder[] = {'d','b','e','g','a','f','c'};
//font order is: abdegcf
GetTreeOrder(BackOrder, 0, 6, CenterOrder, 0, 6);
cout<<endl;
return 0;
}
- 由二叉树的后序和中序求先序
- 由二叉树的后序和中序求层次遍历
- 二叉树由后序,中序求先序
- 由二叉树的后序序列和中序序列构成二叉树
- 【二叉树】 二叉树的计数(由先序和后序遍历得二叉树可能形状个数)
- 由中序和后序重建二叉树
- 由二叉树的先序和中序结果求取后序结果
- 由二叉树的后序和中序结果求取先序结果
- 由二叉树的先序中序推出后序
- 由二叉树的先序中序推出后序(转)
- 由二叉树的先序中序推出后序
- NYOJ 848 STBE【由二叉树的先序和中序建立二叉树并得起后序】
- 由二叉树的前序遍历和后续遍历结果还原二叉树,并后序遍历打印
- 树和二叉树的应用举例(相似二叉树,由先序和中序、中序和后序确定二叉树)
- 由二叉树先序遍历/后序遍历和中序遍历生成二叉树
- NYOJ 756 重建二叉树【由二叉树的后序和中序建立二叉树并得先序】
- 代码实现 由二叉树的先序遍历和中序遍历推出后序遍历
- 由二叉树的先序和中序遍历生成二叉树的层序和后序遍历结构
- Tomcat遇到“Error listenerStart”或“Error filterStart”问题且无详细日志时的log配置.
- OpenCms 在 FireFox3.5 下的一个问题
- 标准之路(网站代码示例)
- 最犇的.Net下的委托入门讲解
- Windows CE的文件系统
- 由二叉树的后序和中序求先序
- MFC 使用MSFlexGrid6.0控件经验
- 关于"从不是创建控件的线程访问它”
- WinCE EBOOT中的Boot Args与Driver Globals
- (Python编程)Pickle对象
- 新手,希望常来踩踩!
- 补充--解决由于Office文档过大导致的传输问题
- 页面跳转三种方法比较
- 如何在VSTS 2010 中自定义真实网络仿真