<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

本文涉及的产品
转发路由器TR,750小时连接 100GB跨地域
简介: 问题描述: A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoi...

问题描述:

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.


输入:

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.

输出:

For each test case print a single line specifying the corresponding postorder sequence.

样例输入:

9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6

样例输出:

7 4 2 8 9 5 6 3 1

思路:

大致题意,已知前序和中序,求后序。根据二叉树的构建思想即可。

代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;   int t;
struct treenote
{
    int elem;
    treenote *lchild;
    treenote *rchild;
};
treenote *gettree(int  *preorder,int  *inorder ,int len)
{
    if(len<=0)
        return NULL;
    treenote *note =new treenote;
    note ->elem =*preorder;
int     root=0;
   while(root<len)
   {
       if(*preorder==inorder[root])
        break;
       root++;
   }
    note->lchild=gettree(preorder+1,inorder,root);
    note->rchild=gettree(preorder+1+root,inorder+root+1,len-root-1);
   len==t? cout<<note->elem :cout<<note->elem<<" ";
    return note;

}
int main()
{

    int preorder[1020],inorder[1020];
    while (cin>>t&&(1<=t<=1000))

   {
       for(int i=0;i<t;i++)
        cin>>preorder[i];
       for(int i=0;i<t;i++)
        cin>>inorder[i];
    gettree(preorder, inorder, t);
    cout<<endl;
   }
    return 0;

}
目录
相关文章
|
Web App开发 前端开发 Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
 Connection reset by peer的常见原因: 1)服务器的并发连接数超过了其承载量,服务器会将其中一些连接关闭;    如果知道实际连接服务器的并发客户数没有超过服务器的承载量,看下有没有网络流量异常。
862 0
|
Web App开发 存储 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
NoSuchObjectException(message:There is no database named cloudera_manager_metastore_canary_test_db_hive_hivemetastore_df61080e04cd7eb36c4336f71b5a8bc4) at org.
1082 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
service cloudera-scm-agent stop service cloudera-scm-agent stop umount /var/run/cloudera-scm-agent/process umo...
764 0
|
Web App开发 前端开发 数据库
|
Web App开发 前端开发
|
Web App开发 前端开发 关系型数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
如果mysql正在运行,/etc/init.d/mysqld stop 启动mysql(无需输入密码):bin/safe_mysqld –skip-grant-tables & 在bin目录下执行mysql,此时无需输入密...
808 0
|
Web App开发 前端开发 数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
数据仓库建设步骤Posted on 2015-03-04 10:18 xuzhengzhu 阅读(1164) 评论(0) 编辑 收藏 1.系统分析,确定主题 确定一下几个因素:    ·操作出现的频率,即业务部门每隔多长时间做一次查询分析。
861 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
在统计分析系统中, 维度:指人们分析事物的角度。比如,分析活跃用户,可以从时间的维度,也可以从地域的维度去看,也可以时间、地域两个维度组合去分析。
668 0