二叉树前序,中序,后序求法
来源:互联网 发布:淘宝怎么提高买家信誉 编辑:程序博客网 时间:2024/06/05 16:29
二叉树的前序,中序,后序序列是根据根节点的访问顺序定义的。
即:
前序序列:根节点 -> 左子树 -> 右子树
中序序列:左子树 -> 根节点 -> 右子树
后序序列:左子树 -> 右子树 -> 根节点
给出中序序列和另外一个序列,就可以求出第三个序列。
例题:POJ - 2255-Tree Recovery(http://poj.org/problem?id=2255)
#include<iostream>#include<cstring>#include<cstdio>using namespace std;void print_postOrder(int n, char *pre, char *in){ if(n <= 0) return; int cnt, p = 0; while(in[p] != pre[0]) p++; print_postOrder(p, pre+1, in);//打印左子树 print_postOrder(n - p - 1, pre + p + 1, in + p + 1);//打印右子树 printf("%c", pre[0]);//打印根节点 ///根据需要输出的序列的不同,把这句话放在不同位置}int main(void){ char inOrder[105], preOrder[105]; while(scanf("%s", preOrder) != EOF) { scanf("%s", inOrder); int n = strlen(preOrder); print_postOrder(n, preOrder, inOrder); printf("\n"); } return 0;}
0 0
- 二叉树前序,中序,后序求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序,中序,后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- Docker学习笔记4.2-Dockerfile指令详述
- Leetcode 111. Minimum Depth of Binary Tree
- IEEE 802.15.4协议完整中文版 - 3.2 缩略语和简写
- C++ map 中的reverse_iterator
- Unix & Linux笔记(16)
- 二叉树前序,中序,后序求法
- websocket服务端PHP代码
- 修改代码
- MAC OS下JDK版本切换指南
- 内外网同时上网的方法
- Android开发:利用AlarmManager不间断向服务器发送请求以及notification通知
- MTSimpleAdapter框架扩展——添加empty页面、error页面和loading等特性
- 同步代码块什么时候释放锁
- xargs的用法