给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树:
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 ...
随机推荐
- Spring,FetchType.LAZY和FetchType.EAGER什么区别?
1.FetchType.LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载. 2.FetchType.EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载. ...
- Unix系统中常用的信号含义
http://blog.csdn.net/u012349696/article/details/50687462 编号为1 ~ 31的信号为传统UNIX支持的信号,是不可靠信号(非实时的),编号为32 ...
- 【BZOJ1833】[ZJOI2010] count 数字计数(数位DP)
点此看题面 大致题意: 求在给定的两个正整数\(a\)和\(b\)中的所有整数中,\(0\sim9\)各出现了多少次. 数位\(DP\) 很显然,这是一道数位\(DP\)题. 我们可以用前缀和的思想, ...
- 【CF1000C】Covered Points Count(离散化+差分)
点此看题面 大致题意: 给出\(n\)条线段,分别求有多少点被覆盖\(1\)次.\(2\)次...\(n\)次. 正常的算法 好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了 ...
- python_39_变量补充
#使用eval()函数计算字符串中的有效表达式,并返回结果 a='1+3' print(eval(a)) b='''{ '闵行': { '人民广场': { '炸鸡店': {}, }, } ''' pr ...
- 【转】转自微信公众号 JavaScript 复杂判断的更优雅写法
与微信公众号看到一篇js复杂判断的文章,对我启发很大,故转到博客园以供后期不断学习并应用于项目.原文地址:https://mp.weixin.qq.com/s/ClFDRj4MnAxv1dJ5VWKS ...
- CXF学习记录
1 apache CXF入门 1.1 下载 官网:cxf.apache.org 下载CXF的开发包: Apache CXF = Celtix + Xfire 支持多种协议: SOAP1.1,1.2 X ...
- 第27题:Leetcode226: Invert Binary Tree反转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 思路 如果根节点存在,就交换两个子树的根节点,用递归 ...
- getchar输入多行字符,原格式输出(包含换行符)
#include<stdio.h> int main() { FILE fp; ]; ; char ch; while((ch=getchar())!=EOF){ str[k++]=ch; ...
- FILE对象线程安全
根据apue讲述: 标准的IO例程可能从它们各自的内部数据结构的角度出发,是以线程安全的方式实现的!但在线程中,如果标准 IO例程都获取它们各自的锁,那么在做一次一个字符的IO时就会出现严重的性能下降 ...