二叉树的恢复
来源:互联网 发布:加百列和路西法知乎 编辑:程序博客网 时间:2024/05/18 01:13
一、已知前序和中序求后序
已知前序的第一个节点为根节点,设为root,start设为当前二叉树的最左边的节点,end为当前二叉树最右边的节点。
#include<iostream>using namespace std;int pre[] = { 1, 2, 3, 4, 5, 6 };int in[] = { 3, 2, 4, 1, 6, 5 };void post(int root, int start, int end) { if (start > end)return; int i = start; while (i < end&&in[i] != pre[root])i++; post(root + 1, start, i - 1); post(root + 1 + i - start, i + 1, end); cout << pre[root] << " ";}int main(){ post(0, 0, 5); return 0;}
二、已知后序和中序求前序
#include<iostream>using namespace std;int last[6] = { 3,4,2,6,5,1 };int in[6] = { 3,2,4,1,6,5 };void levelorder(int root,int start,int end) { if (start > end)return; int i = start; while (i < end&&in[i] != last[root])i++; levelorder(root - end + i-1, start,i-1); cout << last[root] << " "; levelorder(root-1, i + 1, end); }int main(){ levelorder(5,0,5); return 0;}
阅读全文
0 0
- 二叉树的恢复
- 二叉树的恢复
- 二叉树的恢复
- 二叉树的恢复
- 二叉树的恢复
- 二叉树的恢复
- 二叉树的还原恢复
- c语言实现二叉树的恢复
- 二叉树的恢复和遍历
- 由二叉树的遍历序列恢复二叉树
- 恢复二叉树
- 二叉树恢复解析
- 恢复二叉搜索树
- 恢复二叉搜索树
- 二叉树恢复
- 二叉树之恢复
- 恢复被交换二叉搜索树的节点值
- 遍历结果恢复二叉树
- 浏览器页面滚动条美化(样式)
- 腾讯云挂载云硬盘
- exit(0)与exit(1)、return区别
- Win7下U盘安装Ubuntu14.04双系统步骤详解
- iOS 绘制文字和图片
- 二叉树的恢复
- Java中常见的异常
- 汇编代码,小括号和中括号
- android 3.0sdk 以后力推DialogFragment 代替 Dialog
- Linux子进程的异步等待方式(SIGCHLD信号)
- 块元素和内联元素
- ASCII排序
- 第一行代码 U1-U2
- Cocos2DX UIListView 大量加载优化方案