已知 中序&后序  建立二叉树:

SDUT 1489

Description

 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历

Input

 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的中序遍历序列,第二个字符串表示二叉树的后序遍历序列。 

Output

 输出二叉树的先序遍历序列

Sample Input

2
dbgeafc
dgebfca
lnixu
linux

Sample Output

abdegcf
xnliu 代码实现:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string.h>
#include<stack>
#include<set>
#include <queue>
using namespace std;
struct Tree
{
char a;
Tree * l;
Tree *r;
};
//已知中序与后序递归建树::
Tree * ipCreatTree(char *instar,char *inend ,char *poststar,char*postend) //instar 中序首地址 inend 中序尾地址 poststar后序首地址 postend后序尾地址
{
Tree *root = (Tree*)malloc(sizeof(Tree)); root->a = *postend; root->l = NULL; root->r = NULL; if(instar==inend&&poststar==postend) return root; char *inp = instar; while((*inp != root->a)&&(inend-inp>=)) ++inp; int leftLength = inp - instar; char *leftInorderEnd = instar+leftLength-; if(leftLength>)
root->l = ipCreatTree(instar,leftInorderEnd,poststar,poststar+leftLength-);
if(leftLength<inend-instar)
root->r = ipCreatTree(leftInorderEnd+,inend,poststar+leftLength,postend-); return root;
}
void Preorder(Tree *r)
{
if(r == NULL)
return;
else
{
printf("%c",r->a);
Preorder(r->l);
Preorder(r->r);
}
}
int main()
{
int t;
char inorder[];
char postorder[];
Tree *root;
scanf("%d",&t);
getchar();
while(t--)
{
scanf("%s%s",inorder,postorder);
int inlen = strlen(inorder);
int postlen = strlen(postorder);
root=ipCreatTree(inorder,inorder+inlen-,postorder,postorder+postlen-);
Preorder(root);
printf("\n");
}
return ;
}

已知 先序&中序  建立二叉树:

SDUT 3343

Description

给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。

Input

输入数据有多组,每组数据第一行输入 1 个正整数 N(1 <= N <= 50) 为树中结点总数,随后 2 行先后给出先序和中序遍历序列,均是长度为 N 的不包含重复英文字母 ( 区分大小写 ) 的字符串。

Output

  输出一个整数,即该二叉树的高度。

Sample Input

9
ABDFGHIEC
FDHGIBEAC

Sample Output

5
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string.h>
#include<stack>
#include<set>
#include <queue>
using namespace std;
struct Tree
{
char a;
Tree * l;
Tree *r;
};
//已知先序与中序递归建树::
Tree * piCreatTree(char *instar,char *inend ,char *prestar,char*preend) //instar 中序首地址 inend 中序尾地址 poststar先序首地址 postend先序尾地址
{
Tree *root = (Tree*)malloc(sizeof(Tree)); root->a = *prestar; root->l = NULL; root->r = NULL; if(instar==inend&&prestar==preend) return root; char *inp = instar; while((*inp != root->a)&&(inend-inp>=)) ++inp; int leftLength = inp - instar; char *leftInorderEnd = instar+leftLength-; if(leftLength>)
root->l = piCreatTree(instar,leftInorderEnd,prestar+,prestar+leftLength);
if(leftLength<inend-instar)
root->r = piCreatTree(leftInorderEnd+,inend,prestar+leftLength+,preend); return root;
} int Depth(Tree *r)
{
int hl,hr;
if(r==NULL) return ;
hl = Depth(r->l);
hr = Depth(r->r);
if(hl>=hr) return hl+;
else return hr+;
}
int main()
{
int t;
char preorder[];
char inorder[];
Tree *root;
while(~scanf("%d",&t))
{
getchar();
scanf("%s%s",preorder,inorder);
int inlen = strlen(inorder);
int prelen = strlen(preorder);
root=piCreatTree(inorder,inorder+inlen-,preorder,preorder+prelen-);
printf("%d\n",Depth(root));
}
return ;
}

本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.
 

给出 中序&后序 序列 建树;给出 先序&中序 序列 建树的更多相关文章

  1. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  2. LeetCode:二叉树的前、中、后序遍历

    描述: ------------------------------------------------------- 前序遍历: Given a binary tree, return the pr ...

  3. 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别

    前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...

  4. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  5. SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)

    求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description  已知一 ...

  6. SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...

  7. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. 前序+中序->后序 中序+后序->前序

    前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...

  9. C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现【可运行】

    C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现[可运行] #include <stdio.h> #include <stdlib.h> typedef int Key ...

随机推荐

  1. 用户在设置密码时,提醒请输入半角字符(vue+element+valid)

    要保证callback()只有一个出口 rules:{ newPassword: [{validator:(rule,newPassword,callback)=>{ var all = fal ...

  2. SSM框架之动态代理Mapper快速搭建

    1.   新建Maven项目 ssmnew 2.    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  3. solr 近实时搜索

    摘要: Solr的近实时搜索NRT(Near Real Time Searching)意味着文档可以在索引以后马上可以被查询到. Solr不会因为本次提交而阻塞更新操作,不会等待后台合并操作(merg ...

  4. java基础——类加载与反射

    第1章 类加载器 1.1 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. (1)加载 就是指将class文件读入内存,并为之创 ...

  5. Oracle表连接学习笔记

    目录 一.表连接类型 1.1 内连接 1.2 外连接 二.表连接方法 2.1 表连接方法分类 2.2 表连接方法特性区别 @ 一.表连接类型 表连接类型可以分为:内连接.外连接,在看<收获,不止 ...

  6. question 002: dev c++ 当中如何调整字体大小?How to get the first program with C++? c++属于什么软件?

    方法:按住ctrl+鼠标滑轮滚动 c++属于系统软件还是应用软件? 说哪个都不对,编译之前属于应用软件,after compile ,it belongs to system software. #i ...

  7. Too Rich HDU - 5527 (贪心+dfs)

    Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  8. python 写 组合两两组合

    紧挨着 组合  a b c d  ----> ab ,bc ,cd portList = ['a', 'b', 'c', 'd'] for i, p in enumerate(portList) ...

  9. k8s的ingress资源简述

    ingress controller是独立与controller-manager的Ingress的主要作用是可以利用nginx,haproxy,envoy,traefik等负载均衡器来暴露集群内部服务 ...

  10. unix gcc编译过程

    gcc编译过程 现代编译器常见的编译过程: 源文件-->预处理-->编译/优化-->汇编-->链接-->可执行文件 对于gcc而言: 第一步 预处理       命令: ...