Data Structure: 548-tree

Source: Internet
Author: User

 

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 ;}

 

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.