zoukankan      html  css  js  c++  java
  • NYOJ 221 Tree (二叉树)

    题目链接

    描述

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.

    This is an example of one of her creations:

                                                    D
    
                                                  / 
    
                                                 /   
    
                                                B     E
    
                                               /      
    
                                              /        
    
                                             A     C     G
    
                                                        /
    
                                                       /
    
                                                      F
    

    To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.

    She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

    Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.

    However, doing the reconstruction by hand, soon turned out to be tedious.

    So now she asks you to write a program that does the job for her!

    • 输入
      The input will contain one or more test cases. Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.) Input is terminated by end of file.
    • 输出
      For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
    • 样例输入
      DBACEGF ABCDEFG
      BCAD CBAD
    • 样例输出
      ACBFGED
      CDAB

    分析:

    题意其实很简单,就是给定你一棵二叉树的先序和中序序列,然后输出这棵二叉树的后序序列。

    我们知道对于任何有n个节点的二叉树,都可以由它的中序序列和先序序列(后序序列)来唯一的确定。

    对于先序序列,第一个节点必定为该棵树的根节点,然后在中序序列中找到这个节点,即可将该树一份为二,左边的即为根节点的左子树,右边的为根结点的右子树,然后用同样的方法递归寻找左子树和右子树。

    代码:

    #include<string>
    #include<iostream>
    #include<algorithm>
    #include<stdio.h>
    using namespace std;
    struct Node
    {
       char data;
       Node * lchild;
       Node * rchild;
    };
    
    Node *build(string pre,string in)
    {
        Node * root=NULL;
        if(pre.length()>0)
        {
            root=new Node;
            root->data=pre[0];
            int index=in.find(pre[0]);///在中序序列中找到当前树的根节点
            root->lchild=build(pre.substr(1,index),in.substr(0,index));///递归左子树
            root->rchild=build(pre.substr(index+1),in.substr(index+1));///递归右子树
        }
        return root;
    }
    
    void post(Node *root)
    {
        if(root!=NULL)
        {
            post(root->lchild);///先访问左子树
            post(root->rchild);///在访问右子树
            printf("%c",root->data);///最后输出根节点
        }
    }
    int main()
    {
        string pre,in;
        while(cin>>pre>>in)
        {
            Node* root=build(pre,in);///将先序和中序序列转换为后序序列
            post(root);///后序序列递归输出
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    NOIP201105铺地毯
    50148155HYF旅游
    连通性判断
    传递消息1
    找朋友
    5796: 最短Hamilton路径(状压dp)
    2283: A Mini Locomotive(01背包)
    2616: Cow Frisbee Team(01背包)
    2593: Secret Message(字典树)
    Stammering Aliens(二分+Hash 卡过)
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6734865.html
Copyright © 2011-2022 走看看