The reconstruction of the Tree binary Tree in the 548-Tree Model of ultraviolet A (UVa) -- build a Tree by means of the Middle-order traversal and subsequent Traversal

Source: Internet
Author: User

Question:
Two groups of numbers are given on the same binary tree. The first group is output in the inorder order, and the second group is output in the postorder order, build the original binary tree based on the two sets of data, then calculate the sum of numbers from the root node to the path of each leaf node, and output the smallest sum.

Sample input:
3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255

Sample output:
1
3
255

Analysis:
This is the use of Binary Tree Reconstruction and traversal.
Binary tree traversal: first, middle, and then
As long as there is a central sequence followed by another sequence, the original binary tree can be reconstructed uniquely.
After rebuilding a binary tree, you only need to search for this binary tree, obtain the sum of the paths, and find the smallest sum.


The method for creating a tree by means of the forward and forward traversal by means of the forward traversal:
[Cpp]
// Returns the root node pointer and returns the result from the middle and back-order traversal sequences.
Node * InPostCreateTree (int * mid, int * post, int len ){
If (len = 0)
Return NULL;
Int I = len-1;
While (post [len-1]! = Mid [I])
-- I;
Node * h = NewNode ();
H-> data = post [len-1];
H-> left = InPostCreateTree (mid, post, I );
H-> right = InPostCreateTree (mid + I + 1, post + I, len-i-1 );
Return h;
}

// Returns the pointer of the root node by creating the traversal sequence in the forward and middle order.
Node * PreInCreateTree (int * mid, int * pre, int len) // n identifies the length of s2
{
If (len = 0)
Return NULL;

Int I = 0;
While (* mid! = Pre [I])
++ I;
 
Node * h = NewNode ();
H-> data = * mid;
H-> left = PreInCreateTree (mid + 1, pre, I );
H-> right = PreInCreateTree (mid + I + 1, pre + I + 1, len-i-1 );
Return h;
}

 

AC code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <vector>
Using namespace std;
Const int MAXN = 10005;
Int inOrder [MAXN], postOrder [MAXN], nIndex;
 
Class Node {
Public:
Int data;
Node * left;
Node * right;
};
 
Int nodeIndex;
Node node [MAXN];
Vector <int> result;
Vector <Node *> pResult;
Bool flag;
Int ans;
 
Inline Node * NewNode (){
Node [nodeIndex]. left = NULL;
Node [nodeIndex]. right = NULL;
Return & node [nodeIndex ++];
}

Inline void input (){
NIndex = 1;
While (getchar ()! = '\ N ')
Scanf ("% d", & inOrder [nIndex ++]);
// Enter the second row and traverse it in the descending order
For (int I = 0; I <nIndex; ++ I)
Scanf ("% d", & postOrder [I]);
}
 
// Returns the root node pointer and returns the result from the middle and back-order traversal sequences.
Node * InPostCreateTree (int * mid, int * post, int len ){
If (len = 0)
Return NULL;
Int I = len-1;
While (post [len-1]! = Mid [I])
-- I;
Node * h = NewNode ();
H-> data = post [len-1];
H-> left = InPostCreateTree (mid, post, I );
H-> right = InPostCreateTree (mid + I + 1, post + I, len-i-1 );
Return h;
}
 
Void dfs (Node * root, int n ){
If (! Root-> left &&! Root-> right ){
Result. push_back (n + root-> data );
PResult. push_back (root );
Return;
}
If (root-> left) dfs (root-> left, n + root-> data );
If (root-> right) dfs (root-> right, n + root-> data );
}
 
Int main (){
Freopen ("input.txt", "r", stdin );
While (~ Scanf ("% d", & inOrder [0]) {
Input ();
NodeIndex = 0;
Node * root = InPostCreateTree (inOrder, postOrder, nIndex );
Result. clear ();
PResult. clear ();
Dfs (root, 0 );
Int minPos = 0;
For (int I = 1; I <result. size (); ++ I)
If (result [I] <result [minPos]) minPos = I;

Printf ("% d \ n", pResult [minPos]-> data );
}
Return 0;
}

 

Author: shuangde800

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.