zoukankan      html  css  js  c++  java
  • HDU 1710 二叉树三种遍历

    Binary Tree Traversals

    Problem Description
    A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

    In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

    In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

    In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

    Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
     
    Input
    The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
     
    Output
    For each test case print a single line specifying the corresponding postorder sequence.
     
    Sample Input
    9
    1 2 4 7 3 5 8 9 6
    4 7 2 1 8 5 9 3 6
     
    Sample Output
    7 4 2 8 9 5 6 3 1
     
    通过先序中序序列求出后序遍历序列,递归玩的不熟,树是时候系统学习一下了。
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int  num=0;
    struct donser
    {
        int data;
        donser*lson;
        donser*rson;
    };
    donser *root;
    donser *creat(int *a,int *b,int n)//da下标从i开始依次找  db下标从j开始向后扫  n向后扫几个
    {
        donser *ss;
        for(int k=0;k<n;k++)
        {
            if(b[k]==a[0])
            {
                ss=new donser;
                ss->data=b[k];
                ss->lson=creat(a+1,b,k);
                ss->rson=creat(a+k+1,b+1+k,n-k-1);
                return ss;
            }
        }
        return NULL;
    }
    
    void solve(donser *r)
    {
        if(r!=NULL)
        {
            solve(r->lson);
            solve(r->rson);
            if(r!=root) cout<<r->data<<" ";
            else cout<<r->data<<endl;
        }
    }
    
    int main()
    {
        int  da[1010],db[1010];//前中
        while(~scanf("%d",&num))
        {
            for(int i=0;i<num;i++) {scanf("%d",&da[i]);}
            for(int i=0;i<num;i++) {scanf("%d",&db[i]);}
            root=creat(da,db,num);
            solve(root);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    iOS之内存管理(ARC)
    分布式锁1 Java常用技术方案
    谈谈如何使用Netty开发实现高性能的RPC服务器
    前后端分离开发模式下后端质量的保证 —— 单元测试
    jquery实现"跳到底部","回到顶部"效果
    html内容超出了div的宽度如何换行让内容自动换行
    采用easyui+ajax+htm+ashx编写 通过用户对应角色 角色对应菜单 控制用户的访问权限
    javascript [] 与 {} 的区别
    图说设计模式
    T4教程1 T4模版引擎之基础入门
  • 原文地址:https://www.cnblogs.com/dzzy/p/5067971.html
Copyright © 2011-2022 走看看