根据二叉树的中序和后序还原二叉树
来源:互联网 发布:找出两列不同的数据 编辑:程序博客网 时间:2024/05/22 05:12
P:终点时要明白二叉树的几种遍历方法以及规律;
先序:中左右。
中序:左中右。
后序:左右中。
上面的左右皆为中的子树,所以优势具有二叉树的特性,那么我们就可以递归还原树了;
这里是一个二叉树还原及求深度。
/*****************************************Author :Crazy_AC(JamesQi)Time :2015File Name :*****************************************/// #pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <algorithm>#include <iomanip>#include <sstream>#include <string>#include <stack>#include <queue>#include <deque>#include <vector>#include <map>#include <set>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <limits.h>using namespace std;#define MEM(a,b) memset(a,b,sizeof a)#define pk push_backtemplate<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}typedef long long ll;typedef pair<int,int> ii;const int inf = 1 << 30;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;struct node{char data;struct node *l,*r;}*rt;char ins[1000],postt[1000];void Built_Bitree(node *&rt,char *in,char * post,int n){if (n <= 0) {rt = NULL;return ;}int pos = strchr(in,post[n - 1]) - in;rt = new node;rt->data = post[n - 1];Built_Bitree(rt->l,in,post,pos);Built_Bitree(rt->r,in + pos + 1,post + pos,n - pos - 1);return ;}int depth(node *&p){if (!p) return 0;int ld = depth(p->l);int rd = depth(p->r);// free(p->l);// free(p->r);delete p->l;delete p->r;return Get_Max(ld + 1,rd + 1);}int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);int x,n,i;cin >> n;while(n--){scanf("%s%s",ins,postt);Built_Bitree(rt,ins,postt,strlen(postt));printf("%d\n",depth(rt));}return 0;}/*2dbgeafcdgebfcalnixulinux*/
0 0
- 根据二叉树的中序和后序还原二叉树
- 【二叉树】根据二叉树的中序遍历和前序遍历,还原二叉树
- 根据二叉树的后序遍历以及中序遍历还原二叉树
- 根据二叉树的先序序列和中序序列还原二叉树并打印后序序列
- 根据二叉树的先序和中序遍历还原二叉树
- 根据后序遍历和中序遍历结果还原二叉树
- 根据二叉树的前序遍历和中序遍历(或者中序遍历和后序遍历)还原二叉树
- 根据二叉树前序、中序遍历还原二叉树
- LeetCode之通过二叉树的中序遍历和后序遍历还原二叉树
- 二叉树 中序+后序->还原二叉树
- 根据层次遍历和中序遍历的结果还原一颗二叉树
- 根据层次遍历和中序遍历的结果还原一颗二叉树
- 根据先序、中序、后序中的两者还原二叉树——递归函数和Csharp泛型
- 如何根据前序、中序、后序遍历还原二叉树
- 根据后序-中序遍历结果 来还原一颗二叉树
- 根据后序和中序遍历重建二叉树
- 根据后序和中序建立二叉树
- 根据前序遍历和中序遍历还原二叉树
- 白盒测试
- Struts2——Action动态方法调用
- Linux(Ubuntu) Java(Jdk1.8.0_60)安装以及配置
- *LeetCode-Best Time to Buy and Sell Stock II
- Android开发规范
- 根据二叉树的中序和后序还原二叉树
- JavaScript多级菜单含有源码效果图示例
- iOS 完全复制UIView
- 冒泡法
- SSD 下的 MySQL IO 优化
- 带Mingw的code:blocks的安装指南
- 文章标题
- MySQL存储过程详解 mysql 存储过程mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored
- Android的引用jar包