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.node
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.ios
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.post
Output Specification:spa
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.code
Sample Input 1:7 1 2 3 4 6 7 5 2 6 7 4 5 3 1Sample Output 1:
Yes 2 1 6 4 7 3 5Sample Input 2:
4 1 2 3 4 2 4 3 1Sample Output 2:
No 2 1 3 4
分析:目前,關於二叉樹的遍歷算法,有前序和中序,後序和中序,層序和中序,這三種組合都是能夠惟一肯定一顆二叉樹的,目前都有用代碼實現。其中,前序和中序建樹和後序和中序建樹兩種方法都是想辦法分出左右子樹,而後對左右字數進行遞歸建樹。層序和中序也能夠用遞歸實現,可是目前我只用了非遞歸的實現方法。關於今天的建樹方法,使用的是二叉樹的前序遍歷和後序遍歷。可是咱們知道,僅有前序遍歷和後序遍歷是無法肯定一顆二叉樹的。這裏,前序遍歷和後序遍歷在某些調劑下能夠惟一肯定一顆二叉樹。但這裏不作這個要求。題目的要求是當產生歧義是可隨意創建一顆知足前序和後序遍歷的二叉樹便可。blog
目前已經碰到了二叉樹建樹的大多數狀況,下次有時間稍微總結一下。遞歸
#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("\n"); } 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\n"); else printf("No\n"); outans(); return 0; }