给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树:
SDUT 1489
Description
Input
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
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 ;
}
本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.
给出 中序&后序 序列 建树;给出 先序&中序 序列 建树的更多相关文章
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- LeetCode:二叉树的前、中、后序遍历
描述: ------------------------------------------------------- 前序遍历: Given a binary tree, return the pr ...
- 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别
前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)
求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 已知一 ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现【可运行】
C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现[可运行] #include <stdio.h> #include <stdlib.h> typedef int Key ...
随机推荐
- 面试中常见的 MySQL 考察难点和热点
基本架构 MySQL是典型的三层架构模式,在平常使用中对MySQL问题排查和优化,也应该针对具体问题,从对应的层解决问题 服务层:经典的C/S架构,主要是处理连接和安全验证. 核心层:处理MySQL核 ...
- Count Numbers(矩阵快速幂)
Count Numbers 时间限制: 8 Sec 内存限制: 128 MB提交: 43 解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...
- python_25_string
name="my name is 齐志光qizhiguang" print(name.capitalize())#首字母变大写 print(name.count('i'))#统计字 ...
- python_21_copy
import copy person=['name',['saving',100]] #3种浅copy方式 p1=copy.copy(person) p2=person[:] p3=list(pers ...
- python 线程的调用方式
python 线程的调用方式 #!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/5/24 9:44 # ...
- jQuery Pagination分页插件--无刷新
源码:https://github.com/SeaLee02/FunctionModule/blob/master/UploadFiles/WebDemo/FenYE/FenYeAjax.aspx 代 ...
- 优化你的java代码性能
一.避免在循环条件中使用复杂表达式 在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快. 例子:import java.util. ...
- Oracle Undo 和 Redo
1. REDO(重做信息) Redo log file(重做日志文件),是数据库的事务日志. Oracle维护着两类重做日志文件:在线(online)重做日志文件和归档(archived)重做日志文件 ...
- eclipse关闭无用启动项,降低内存占用
1,我使用的eclipse版本 2.打开windows-->preference 3,勾选掉无用的启动项,我的已经去掉过了, 4,重启eclipse,如果操作后导致一些必须的功能不能用了,可以点 ...
- springboot中加入druid对sql进行监控
springboot作为现在十分流行的框架,简化Spring应用的初始搭建以及开发过程,现在我们就使用springboot来进行简单的web项目搭建并对项目sql进行监控. 项目的搭建就省略了,spr ...