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

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. 从用户访问网站流程开始,细说web网络基础

    1.用户访问网站流程框架 2.dns解析原理 3.tcp/ip三次握手过程原理,11种连接状态 4.tcp/ip四次挥手过程原理,11种连接状态 5.http协议原理(www服务的请求过程)请求细节, ...

  2. js数据结构处理--------树结构数据遍历

    1.深度遍历 深度遍历利用栈来实现 class Stack { constructor () { this.top = 0, // 栈的长度 this.list = [] } push(item) { ...

  3. Restful API 的设计规范

    RESTful 是目前最流行的 API 设计规范,用于 Web 数据接口的设计.降低开发的复杂性,提高系统的可伸缩性. Restful API接口规范包括以下部分: 一.协议 API与用户的通信协议, ...

  4. C#关系运算符

    一.C#关系运算符 C#语言的关系运算符是对操作数的比较运算. 二.示例 using System;using System.Collections.Generic;using System.Linq ...

  5. 使用 RuPengGame游戏引擎包 建立游戏窗体 如鹏游戏引擎包下载地址 Thread Runnable 卖票实例

    package com.swift; import com.rupeng.game.GameCore;//导入游戏引擎包 //实现Runnable接口 public class Game_RuPeng ...

  6. JQuery模拟点击页面上的所有a标签,触发onclick事件

    注意: 这种方法需要给所有的a标签加上id属性 页面加载完成模拟点击所有的a标签: <script> $(function () { // 模拟点击页面上的所有a标签,触发onclick事 ...

  7. 十二、MySQL 查询数据

    MySQL 查询数据 MySQL 数据库使用SQL SELECT语句来查询数据. 你可以通过 mysql> 命令提示窗口中在数据库中查询数据,或者通过PHP脚本来查询数据. 语法 以下为在MyS ...

  8. matplotlib(二)——matplotlib控制坐标轴第一个刻度到原点距离

    一.问题描述 具体问题是: 用python库matplotlib进行数据的图表展示: 横坐标是自定义统计值: 保存矢量图(svg),保存后发现横坐的第一个点离坐标原点距离较大,导致图形离y轴较远,让画 ...

  9. rpc - 接口返回数据结构的设计

    方案一: 系统级状态  .业务级别的状态同用 code要特殊声明保留状态,如若不声明保留状态,一旦业务开发人员用到了系统级的状态,就有必要侵入的改动业务返回的code(新code与业务欲返回的code ...

  10. I Like for You to Be Still【我会一直喜欢你】

    I Like for You to Be Still I like for you to be still 我会一直喜欢这你 It is as though you are absent 就算你并不在 ...