HDU1710(二叉树遍历)
来源:互联网 发布:新理念外语网络答案 编辑:程序博客网 时间:2024/06/06 03:55
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2198 Accepted Submission(s): 964
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.
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
91 2 4 7 3 5 8 9 64 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1
刚做时不会,没有想到递归,后来看了大神的代码
#include <stdio.h>#include <stdlib.h>typedef struct BiTNode{ int data; struct BiTNode *lchild; struct BiTNode *rchild;} BiTNode;void PostOrderTraverse(BiTNode *pT, int *flag){ if (pT == NULL) return; PostOrderTraverse(pT->lchild, flag); PostOrderTraverse(pT->rchild, flag); if (*flag) { printf(" %d", pT->data); } else { printf("%d", pT->data); *flag = 1; }}/*BiTNode *CreateBiTreePreIn(int *PreOrder, int *InOrder, int n){ int i; BiTNode *pT; for (i = 0; i < n; ++i) { if (PreOrder[0] == InOrder[i]) { pT = (BiTNode *)malloc(sizeof(BiTNode)); pT->data = PreOrder[0]; ///////////////做孩子一定在左边//////////////////////////////// pT->lchild = CreateBiTreePreIn(PreOrder + 1, InOrder, i); ///////////////////////////////////////////////////////////////// pT->rchild = CreateBiTreePreIn(PreOrder + i + 1, InOrder + i + 1, n - i - 1); return pT; } } return NULL;}*/BiTNode *CreateBiTreePreIn(int *PreOrder, int *InOrder, int n){ BiTNode *p; int i; for(i=0;i<n;i++) { if(PreOrder[0]==InOrder[i]) { p=new BiTNode; p->data=PreOrder[0]; p->lchild=CreateBiTreePreIn(PreOrder+1,InOrder,i); p->rchild=CreateBiTreePreIn(PreOrder+i+1,InOrder+i+1,n-i-1); return p; } } return NULL;}void DestroyBiTree(BiTNode **pT){ if (*pT == NULL) return; DestroyBiTree(&(*pT)->lchild); DestroyBiTree(&(*pT)->rchild); free(*pT);}int main(){ int n, PreOrder[1000], InOrder[1000], i, flag; BiTNode *pT; while (scanf("%d", &n) != EOF) { for (i = 0; i < n; ++i) scanf("%d", PreOrder + i); for (i = 0; i < n; ++i) scanf("%d", InOrder + i); flag = 0; pT =CreateBiTreePreIn(PreOrder, InOrder, n); PostOrderTraverse(pT, &flag); DestroyBiTree(&pT); printf("\n"); } return 0;}
- HDU1710(二叉树遍历)
- hdu1710二叉树后序遍历
- 数据结构之二叉树的遍历 HDU1710 Binary Tree Traversals
- HDU1710 二叉树的前、中、后遍历
- hdu1710 Binary Tree Traversals(二叉树遍历)
- HDU1710 Binary Tree Traversals(DFS,二叉树的遍历)
- HDu1710(二叉树)
- HDu1710(二叉树)
- hdu1710 二叉树详解
- HDU1710二叉树构造
- HDU1710 Binary Tree Traversals【树的遍历】
- HDU1710(根据前序和中序生成二叉树)
- HDU1710——Binary Tree Traversals(二叉树)
- HDU1710 Binary Tree Traversals(二叉树重建模板609)
- 第一次写树hdu1710
- hdu1710
- hdu1710
- hdu1710
- HDU1509(优先级队列+模拟)
- xml
- iframe和父页,window.open打开页面之间的引用
- Windows Touch 笔势
- 在Ecplise上搭建C\C++环境
- HDU1710(二叉树遍历)
- 缓慢渐变维度的处理方式
- VIJOS-P1294-拯救OIBH总部
- something about pedestrian detection
- HTML常用标签学习
- MFC只能运行一个实例
- HTML常见标签学习
- 关于c++中的文件操作
- MessageDigest的功能及用法