Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 489
Question meaning:
A binary tree is given by traversing in the middle and back order, allowing you to find the value of the leaf node with the smallest sum from the root node to the leaf node.
Solution:
Build a binary tree based on the ordinal traversal sequence and the ordinal traversal sequence, and then use DFS to find the value of the leaf node with the smallest sum.
The last node in the Post-order traversal must be the root node, and the node before the root node's position in the middle order must be on the left Tree of the root node, the node after the root node is in the middle order must be in the right tree of the root node.
We can use recursion to create this tree. Note that some nodes do not have left or right children.
Code:
# Include <iostream> # include <cstdio> # define Inf (1 <20) using namespace STD; int inorder [11000], suborder [11000]; struct node {int value, left, right;} tree [11000]; int num, Min, ans; int build (INT start1, int end1, int start2, int end2) // create a binary tree {If (end1 <start1 | end2 <start2) // No left or right children return 0; tree [++ num]. value = suborder [end2]; // Save the value to the Tree node int temp = num; If (start1 = end1) // reach the leaf node {tree [num]. left =-1; return num;} int I; fo R (I = start1; I <= end1; I ++) // traverse the middle order and locate the current node position if (inorder [I] = suborder [end2]) break; tree [temp]. left = build (start1, I-1, start2, i-start1 + start2-1); // create the left Tree of the current node, note that tree [num] cannot be used. left, should be num as the global variable, will change until the end, WA many times tree [temp]. right = build (I + 1, end1, i-start1 + start2, end2-1); // create the right tree return temp for the current node; // note that the return current node number, wa several times} void DFS (INT cur, int sum) // DFS sum {sum + = tree [cur]. value; If (tree [cur]. left =-1) // If the leaf node is reached, {If (s) is returned. Um <min) {min = sum; ans = tree [cur]. value;} return;} If (tree [cur]. left = 0 & tree [cur]. right! = 0) // when there is no child, you cannot do it all. Consider another child {DFS (tree [cur]. right, sum); return;} If (tree [cur]. right = 0 & tree [cur]. left! = 0) {DFS (tree [cur]. left, sum); return;} If (tree [cur]. right = 0 & tree [cur]. left = 0) // return; DFS (tree [cur]. left, sum); // left child dfsdfs (tree [cur]. right, sum); // from the right child dfsreturn;} int main () {char C; int len1 = 0, len2 = 0; while (scanf ("% d % C", & inorder [++ len1], & C )! = EOF) // note the Read mode {While (C! = '\ N') scanf ("% d % C", & inorder [++ len1], & C); do {scanf ("% d % C ", & suborder [++ len2], & C) ;}while (C! = '\ N'); num = 0; build (1, len1, 1, len2); // build min = inf; DFS (1, 0 ); // The value of the leaf node that calculates the maximum sum: printf ("% d \ n", ANS); len1 = len2 = 0; // note that each time it is cleared} return 0 ;}