真二叉树重构(Proper Rebuild)
Description
In general, given the preorder traversal sequence and postorder traversal sequence of a binary tree, we cannot determine the binary tree.
Figure 1
In Figure 1 for example, although they are two different binary tree, their preorder traversal sequence and postorder traversal sequence are both of the same.
But for one proper binary tree, in which each internal node has two sons, we can uniquely determine it through its given preorder traversal sequence and postorder traversal sequence.
Label n nodes in one binary tree using the integers in [1, n], we would like to output the inorder traversal sequence of a binary tree through its preorder and postorder traversal sequence.
Input
The 1st line is an integer n, i.e., the number of nodes in one given binary tree,
The 2nd and 3rd lines are the given preorder and postorder traversal sequence respectively.
Output
The inorder traversal sequence of the given binary tree in one line.
Example
Input
5
1 2 4 5 3
4 5 2 3 1
Output
4 2 5 1 3
Restrictions
For 95% of the estimation, 1 <= n <= 1,000,00
For 100% of the estimation, 1 <= n <= 4,000,000
The input sequence is a permutation of {1,2…n}, corresponding to a legal binary tree.
Time: 2 sec
Memory: 256 MB
Hints
Figure 2
In Figure 2, observe the positions of the left and right children in preorder and postorder traversal sequence.
描述
一般来说,给定二叉树的先序遍历序列和后序遍历序列,并不能确定唯一确定该二叉树。
(图一)
比如图一中的两棵二叉树,虽然它们是不同二叉树,但是它们的先序、后序遍历序列都是相同的。
但是对于“真二叉树”(每个内部节点都有两个孩子的二叉树),给定它的先序、后序遍历序列足以完全确定它的结构。
将二叉树的n个节点用[1, n]内的整数进行编号,输入一棵真二叉树的先序、后序遍历序列,请输出它的中序遍历序列。
输入
第一行为一个整数n,即二叉树中节点的个数。
第二、三行为已知的先序、后序遍历序列。
输出
仅一行,给定真二叉树的中序遍历序列。
样例
见英文题面
限制
对于95%的测例:1 ≤ n ≤ 1,000,000
对于100%的测例:1 ≤ n ≤ 4,000,000
输入的序列是{1,2…n}的排列,且对应于一棵合法的真二叉树
时间:2 sec
空间:256 MB
提示
观察左、右孩子在先序、后序遍历序列中的位置
重温视频05e5-3
目测一辈子也忘不了二叉树重构怎么写了…………
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include<iostream> using namespace std; int n; int * preorder; int * postorder; bool first_output; struct node{ int data; node * left; node * right; }; int findpos(int * ptr_to_array,int value,int start,int end){ //[start,end] for(int i=start;i<=end;i++){ if(ptr_to_array[i]==value)return i; } return -1; } bool MakeTree(int pre_start,int pre_end,int post_start,int post_end,node * CURRENT){// preorder[pre_start,pre_end];postorder[post_start,post_end] if(pre_start>pre_end||post_start>post_end){ return false; } int V,L,R; V=preorder[pre_start]; CURRENT->data=V; if(pre_start==pre_end&&post_start==post_end){ CURRENT->left=NULL; CURRENT->right=NULL; return true; } L=preorder[pre_start+1]; R=postorder[post_end-1]; int L_pos_in_postorder=findpos(postorder,L,post_start,post_end); int R_pos_in_preorder=findpos(preorder,R,pre_start,pre_end); node * left=new node; node * right=new node; CURRENT->left=left; CURRENT->right=right; bool lflag=MakeTree(pre_start+1,R_pos_in_preorder-1,post_start,L_pos_in_postorder,left); bool rflag=MakeTree(R_pos_in_preorder,pre_end,L_pos_in_postorder+1,post_end-1,right); if(!lflag){ delete left; CURRENT->left=NULL; } if(!rflag){ delete right; CURRENT->right=NULL; } return true; } void Inorder_traversal(node * ptr){ if(ptr==NULL)return; Inorder_traversal(ptr->left); if(first_output){ cout<<ptr->data; first_output=false; }else{ cout<<" "<<ptr->data; } Inorder_traversal(ptr->right); } int main(){ first_output=true; ios::sync_with_stdio(false); cin>>n; preorder=new int[n+1]; postorder=new int[n+1]; for(int i=1;i<=n;i++){ cin>>preorder[i]; } for(int i=1;i<=n;i++){ cin>>postorder[i]; } node * ROOT=new node; MakeTree(1,n,1,n,ROOT); Inorder_traversal(ROOT); } |