Uva548 -- tree

Source: Internet
Author: User

Tree

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. the value of a path is the sum of values of nodes along that path.
Input
The input file will contain in a description of the binary tree given as the inorder and postorder traversal sequences of that tree. your program will read two line (until end of file) from the input file. the first line will contain in the sequence of values associated with an inorder traversal of the tree and the second line will contain in the sequence of values associated with a postorder traversal of the tree. all values will be different, greater than zero and less than 10000. you may assume that no binary tree will have more than 10000 nodes or less than 1 node.
Output
For each tree description you shoshould output the value of the leaf node of a path of least value. in the case of multiple paths of least value you shoshould pick the one with the least value on the terminal node.
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

Question:

Input the central and posterior orders of a binary tree and output a leaf node. The sum of the values from the leaf node to the root node is the smallest.

Ideas:

First, create a binary tree in the ascending and middle order, and search through DFS to find the leaves that meet the requirements of the questions. (Global variables are required to record the minimum and leaf values in the DFS process)

Create a binary tree in the middle and back order:

Uses recursion to establish the node gradually. The root node of the branch in the current recursion is determined by the backward order, and then the root position is found in the middle order. Then, the left of the root in the middle order is the left subtree.Arrange in the middle orderIn the middle order of the right subtree. If the length of the Left subtree is Len, the first LEN data in the current post-order is of the Left subtree.Sort in descending order. Perform recursion in the same way. The same applies to the right subtree.

DFS:

Set a variable M, and call it as a real parameter in each recursion, and execute m + = tree. data to ensure that M stores the sum of the current leaf to the root node, and selects the leaf node that matches the meaning of the question based on the size of M.

Code:

1 # include <malloc. h> 2 # include <iostream> 3 # include <stdio. h> 4 # include <string> 5 # include <cstring> 6 using namespace STD; 7 struct tree 8 {9 int data; 10 int left; 11 int right; 12} t [100100]; // array-simulated Binary Tree 13 int m_sum = 100000000, Pos; // saved result 14 int mid [100100], last [100100] when used for DFS; 15 int create_tree (INT M1, int m2, int L1, int l2) 16 {17 if (M1 = m2) 18 {19 T [M1]. left = T [M1]. right =-1; 20 t [M1]. data = mid [M1]; 21 re Turn M1; 22} 23 if (M1> m2) 24 return-1; 25 int I; 26 for (I = 0; I <= m2; I ++) 27 if (mid [I] = last [L2]) break; 28 t [I]. left = create_tree (M1, I-1, L1, l1 + i-m1-1); // recursive build !! Pay attention to the four parameters. The runtime is 29 T [I] many times. right = create_tree (I + 1, M2, l1 + i-m1, l2-1); 30 t [I]. data = mid [I]; 31 return I; 32} 33 int min (int A, int B) 34 {35 return A> B? B: A; 36} 37 int DFS (INT head, int m) 38 {39 m + = T [head]. data; 40 if (T [head]. left =-1 & T [head]. right =-1) 41 {42 if (M <m_sum) // hits the table, selects the smallest result, and saves the value of the leaf node to the POs 43 {44 m_sum = m; 45 Pos = T [head]. data; 46} 47 return T [head]. data; 48} 49 int sum = T [head]. data; 50 if (T [head]. left! =-1 & T [head]. right =-1) sum + = DFS (T [head]. left, m); 51 else if (T [head]. right! =-1 & T [head]. left =-1) sum + = DFS (T [head]. right, m); 52 else sum + = min (DFS (T [head]. left, m), DFS (T [head]. right, M); 53 return sum; 54} 55 int main () 56 {57 int k1 = 0, K2 = 0; 58 While (scanf ("% d ", & Mid [0])! = EOF) 59 {60 Pos = 0, m_sum = 100000000; 61 k1 = 1; 62 for (INT I = 0; I <= 10000; I ++) 63 t [I]. left = T [I]. right =-1, t [I]. data = 0; 64 While (1) 65 {66 char CH = getchar (); 67 If (CH = '\ n') break; 68 scanf ("% d ", & Mid [K1 ++]); 69} 70 K1 --, K2 = 0; 71 while (1) 72 {73 scanf ("% d ", & last [K2 ++]); 74 char CH = getchar (); 75 if (CH = '\ n') break; 76} 77 K2 --; 78 int t_head = create_tree (0, K1, 0, K2); 79 DFS (t_head, 0); 80 printf ("% d \ n", POS ); 81} 82 return 0; 83}

 

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.