数据结构之 二叉树---求二叉树后序遍历和层次遍历(先建树,再遍历)

数据结构实验之求二叉树后序遍历和层次遍历

Time Limit: 1000MS Memory limit: 65536K

题目描述

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

输入

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

输出

每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列

示例输入

2
abdegcf
dbgeafc
xnliu
lnixu

示例输出

dgebfca
abcdefg
linux
xnuli

 第一种方法:根据字符串的递推, 由先序、中序直接计算后序串! 在建树,进行层次遍历!         代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

typedef struct node
{
    char a;
    struct node *ll;
    struct node *rr;
}Binode, *tree;

void build(int len, char *s1, char *s2, char *s) //先序中序 推 后序
{
    if(len<=0)
      return;
    int pos; //位置
    int i;

    for(i=0; i<len; i++)
    {
        if(s2[i]==s1[0])
        {
            pos=i;
            break;
        }
    }
    build(pos, s1+1, s2, s );
    build(len-pos-1, s1+pos+1, s2+pos+1, s+pos );
    s[len-1]=s1[0];
}  

//根据先序中序建树
struct node*creat(struct node *root, char *s, char *s1, int n)
{
    if(n<=0)
      return NULL;
    root=new node;
    root->a=s[0];

    int p=strchr(s1, s[0] )-s1;

    root->ll=creat( root->ll, s+1, s1, p );
    root->rr=creat( root->rr, s+p+1, s1+p+1, n-p-1 );

    return root;
}

//层次遍历 二叉树
void Level_Order(tree root)
{
    queue<tree>q;
    tree qq;

    if(root==NULL)
      return ;
    Binode *p=root;
    cout<<(p->a);

    if(p->ll)
      q.push( p->ll );
    if(p->rr)
      q.push( p->rr );

    while(!q.empty())
    {
        qq=q.front();
        q.pop();
        cout<<qq->a;
        if(qq->ll)
          q.push(qq->ll);
        if(qq->rr)
          q.push(qq->rr);
    }
    return;
}
int main(){
    int t;
    cin>>t;
    char s1[100], s2[100];
    char s3[100];
    while(t--)
    {
        memset(s3, ‘\0‘, sizeof(s3));

        scanf("%s", s1); //中序
        scanf("%s", s2); //后序
        int len=strlen(s1);
        build(len, s1, s2, s3);
        printf("%s\n", s3); //输出后序遍历

        //计算层次遍历
        tree root, tt;
        root=creat(tt, s1, s2, len);

        Level_Order( root);
        cout<<endl;
    }
    return 0;
}
第二种方法:先根据先序、中序建树,在进行后序遍历和层次遍历   代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

typedef struct node
{
    char a;
    struct node *ll;
    struct node *rr;
}Binode, *tree;

//根据先序中序建树
struct node*creat(struct node *root, char *s, char *s1, int n)
{
    if(n<=0)
      return NULL;
    root=new node;
    root->a=s[0];

    int p=strchr(s1, s[0] )-s1;

    root->ll=creat( root->ll, s+1, s1, p );
    root->rr=creat( root->rr, s+p+1, s1+p+1, n-p-1 );

    return root;
}

void postorder(tree p)
{
    if(p)
    {
        postorder(p->ll);
        postorder(p->rr);
        printf("%c", p->a );
    }
}

//层次遍历 二叉树
void Level_Order(tree root)
{
    queue<tree>q;
    tree qq;

    if(root==NULL)
      return ;
    Binode *p=root;
    cout<<(p->a);

    if(p->ll)
      q.push( p->ll );
    if(p->rr)
      q.push( p->rr );

    while(!q.empty())
    {
        qq=q.front();
        q.pop();
        cout<<qq->a;
        if(qq->ll)
          q.push(qq->ll);
        if(qq->rr)
          q.push(qq->rr);
    }
    return;
}

int main()
{
    int t;
    cin>>t;
    char s1[100], s2[100];
    char s3[100];

    while(t--)
    {
        memset(s3, ‘\0‘, sizeof(s3));

        scanf("%s", s1); //中序
        scanf("%s", s2); //后序
        int len=strlen(s1);

        //计算层次遍历
        tree root, tt;
        root=creat(tt, s1, s2, len);
        postorder(root);
        cout<<endl;

        Level_Order( root);
        cout<<endl;
    }
    return 0;
}

     
时间: 2024-08-25 00:38:04

数据结构之 二叉树---求二叉树后序遍历和层次遍历(先建树,再遍历)的相关文章

数据结构实验之求二叉树后序遍历和层次遍历

数据结构实验之求二叉树后序遍历和层次遍历 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历. 输入 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据.每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列. 输出 每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列 示例输入 2

二叉树前序、后序和后序遍历(非递归实现)

二叉树前序.后序和后序遍历(非递归实现) (1)前序     我们知道,前序遍历的顺序是根左右,当根节点不为空时,该节点才可以被打印.目前书上常见对树的遍历都是采用递归的方法实现的,我们知道递归必然会产生中断,也就是有现场信息的保存,如果要实现非递归,那么我们必须自己要有一个栈,用来保存现场信息. 我先给出实现的伪代码,稍后我将解释为什么需要这么做,为何要用到这些条件. 伪代码如下: 1 void PreOrderTraverse(BinaryTree root) 2 { 3    Binary

【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

[145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive soluti

根据二叉树的先序序列和中序序列还原二叉树并打印后序序列

#include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; struct Node { int value; Node *left; Node *right; Node(int value) { this->value = value; left = right = NULL; } }; bool bNotTree = false; Node* RebuildTree(i

SDUTOJ 2173 数据结构实验之求二叉树后序遍历和层次遍历

#include<iostream> #include<stdio.h> #include<queue> #include<string.h> #include<stdlib.h> using namespace std; char s1[100],s2[100],ans[100]; typedef struct BiTNode { char data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTr

已知二叉树前、中序遍历,求后序 / 已知二叉树中、后序遍历,求前序

void solve(int start,int end,int root) { // 前序和中序 -> 后序 // 每次调用solve()函数,传入pre-order的start,end,root if (start > end) // 递归边界 return; int i = start; while (i < end && in.at(i) != pre.at(root)) // 找到左右子树的分割点 i++; solve(start, i - 1, root +

二叉树搜索树的后序遍历序列

题目: 输入一个整型数组,判断该数组是不是二叉搜索树的后序遍历结果. 如果是,返回true.否则返回false 解答: 1 public class Solution { 2 3 public static boolean VerifySquenceOfBST(int[] squence) { 4 return VerifySquenceOfBST(squence, squence.length); 5 } 6 7 private static boolean VerifySquenceOfBS

飘逸的python - 极简的二叉树前中后序通杀函数

对于任一结点,可以按某种次序执行三个操作: 访问结点本身(N) 遍历该结点的左子树(L) 遍历该结点的右子树(R) 用来表示顺序,即,前序NLR/中序LNR/后序LRN. 下面我们用namedtuple来表达树,而通杀的遍历函数带一个order参数,只要我们把指定顺序传进去即可实现对应的遍历. #coding=utf-8 ''' 1 / / / 2 3 / \ / 4 5 6 / / 7 8 9 ''' from collections import namedtuple from sys im

作业 树和森林 遍历(递归/非递归先序,递归/非递归后序,递归层次)

1 #include <iostream> 2 #include"queue.h"//之前写的类 3 #include"stack.h" //之前写的类 4 using namespace std; 5 6 template <class T> 7 class Tree; 8 9 //======================================== 10 // 树节点类声明 11 template <class T>