hdu 1710二叉树的遍历

Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3023    Accepted Submission(s): 1350


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
 

Source


教练前天讲的,昨天还问了学长,但是还是搞到凌晨一点,上午还翘了一节英语课才彻底搞懂,囧—_—|||。。。

彻底弄懂之后感觉收获还是挺大的,不仅对二叉树遍历更熟练,而且对dfs有了更深的理解,应该是到目前为止效果最好的一道题,看来数量的前提还是质量

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int t1[1005];
int t2[1005];
void dfs(int a, int b, int n, int flag)///比较重要的是参数n,表示的是当前结点的子树上有多少个结点,flag只是一个用来控制格式的变量。
{
    int i;
    if(n == 1)///表示当前子树如果只有一个结点就不用再继续搜索了,直接输出,返回上一层。
    {
        printf("%d ", t1[a]);
        return;
    }

    else if(n == 0) return;///表示当前的结点没有子树,如果不存在左子树或右子树就返回上一层,如果是在左子树搜索过程中n=0,则说明该结点没有左子树,反之,若在右子树搜索过程中n = 0,则说明改结点没有右子树。

    for(i=0; t1[a] != t2[b + i]; ++i)///因为前序是“根左右”的顺序,所以当前的t1[a]即为当前的根结点,这句话的作用是找到当前的根结点在t2中的位置。

    dfs(a + 1, /**因为t1为前序,所以当前t1[a+1]元素表示t1[a]的左子树中的下一个根结点**/b, i, 0);///对左子树的搜索
    dfs(a + i + 1, b + i + 1, n - i - 1, 0);///同理,对右子树的搜索。

    if(flag == 1) printf("%d", t1[a]);///当flag == 1的时候,表示是最原始的根结点,应该在后序中最后一个输出。
    else printf("%d ", t1[a]);///一般的根节点,如果进入这个else,则说明该结点的左右子树均已遍历完,输出的是该结点的根。
}
int main()
{
    int T;
    while(scanf("%d", &T) != EOF)
    {
        for(int i=1; i<=T; ++i)
            scanf("%d", &t1[i]);
        for(int i=1; i<=T; ++i)
            scanf("%d", &t2[i]);
        dfs(1, 1, T, 1);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值