zoukankan      html  css  js  c++  java
  • [二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)

    1119. Pre- and Post-order Traversals (30)

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

    Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, first printf in a line "Yes" if the tree is unique, or "No" if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

    Sample Input 1:
    7
    1 2 3 4 6 7 5
    2 6 7 4 5 3 1
    
    Sample Output 1:
    Yes
    2 1 6 4 7 3 5
    
    Sample Input 2:
    4
    1 2 3 4
    2 4 3 1
    
    Sample Output 2:
    No
    2 1 3 4

    分析:目前,关于二叉树的遍历算法,有前序和中序,后序和中序,层序和中序,这三种组合都是可以唯一确定一颗二叉树的,目前都有用代码实现。其中,前序和中序建树和后序和中序建树两种方法都是想办法分出左右子树,然后对左右字数进行递归建树。层序和中序也可以用递归实现,但是目前我只用了非递归的实现方法。关于今天的建树方法,使用的是二叉树的前序遍历和后序遍历。但是我们知道,仅有前序遍历和后序遍历是没法确定一颗二叉树的。这里,前序遍历和后序遍历在某些调剂下可以唯一确定一颗二叉树。但这里不做这个要求。题目的要求是当产生歧义是可随意建立一颗满足前序和后序遍历的二叉树即可。

      目前已经碰到了二叉树建树的大多数情况,下次有时间稍微总结一下。

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    vector<int > pre,post,in;
    
    struct Node
    {
        int data;
        Node *l,*r;
    };
    
    bool flag=true;
    
    void creat(Node * & root,int preL,int preR,int postL,int postR)
    {
        if(preL==preR)
        {
            root=new Node;
            root->data=pre[preL];
            root->l=root->r=NULL;
            return ;
        }
        root=new Node;
        root->data=pre[preL];
        root->l=root->r=NULL;
        int i,j;
        for(i=postL;i<=postR;i++)
        {
            if(post[i]==pre[preL+1])
            {
                break;
            }
        }
        int leftNum=i-postL;
        if(post[i]==post[postR-1])//不确定的条件,无法区分是左子树还是右子树 
        {
            flag=0;
            creat(root->l,preL+1,preR,postL,postR-1);
        }
        else
        {
            creat(root->l,preL+1,preL+1+leftNum,postL,postL+leftNum);
            creat(root->r,preL+leftNum+2,preR,postL+leftNum+1,postR-1);
        }
    }
    
    vector<int> ans;
    
    void inOrder(Node * root)
    {
        if(root!=NULL)
        {
            inOrder(root->l);
            ans.push_back(root->data);
            inOrder(root->r);
        }
    }
    
    void outans()
    {
        for(int i=0;i<ans.size();i++)
        {
            if(i>0) printf(" ");
            printf("%d",ans[i]);
        }
        printf("
    ");
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            int k;
            scanf("%d",&k);
            pre.push_back(k);
        }
        for(int i=0;i<n;i++)
        {
            int k;
            scanf("%d",&k);
            post.push_back(k);
        }
        Node * root=NULL;
        creat(root,0,n-1,0,n-1);
        inOrder(root);
        if(flag==true) printf("Yes
    ");
        else printf("No
    ");
        outans();
        return 0;
    }
  • 相关阅读:
    Web前端框架与类库的思考【转】
    mouseover事件mouseenter事件
    11_Eclipse中演示Git版本号的创建,历史版本号的改动,创建分支,合并历史版本号和当前版本号
    实现了私聊和群聊功能的聊天工具
    有预处理命令#define声明一个常数,用以表明1年中有多少秒
    解决Office软件冲突问题
    pig载入两个不同字段个数的文件?load file with different items(f1有42列,f2有43列读到一个对象中)
    游戏公司通用屏蔽字列表
    Android统计图表MPAndroidChart
    HTTP状态码解析
  • 原文地址:https://www.cnblogs.com/xiongmao-cpp/p/6498672.html
Copyright © 2011-2022 走看看