Example 6-8 Tree UVa548

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Problem-solving ideas: The subject gives a binary tree in the middle sequence traversal and post-order traversal, request to find a leaf, so that it reaches the root node of the right and the smallest, if there are many solutions, then the right of the leaves themselves should be as small as possible. First of all, according to the sequence traversal and post-order traversal to establish a two-fork tree, the problem uses an array to hold the node value of the left and right subtree, the root of the left subtree node for root is Lch[root], the tree node is rch[root].

So, how to follow the sequence traversal, the order of the historical achievements? The method is to find the root based on the post-order traversal, then find the tree roots in the middle sequence traversal, then find the node list of the left and right subtree, and then construct the left-right sub-tree recursively. Finally, the optimal leaf node is solved by first order traversal.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 10000+10int In_order[n], post_ Order[n], Lch[n], Rch[n];int n;bool read_list (int*a) {string line;if (!getline (CIN, line)) return False;stringstream SS ( Line) n = 0;int x;while (ss >> x) a[n++] = X;return n > 0;} int build (int L1, int R1, int L2, int R2)//Achievement {if (L1 > R1) return 0;int root = Post_order[r2];int p = l1;while (in_order [P] = root) p++;int cnt = p-l1;lch[root] = Build (L1, P-1, L2, L2 + cnt-1);//recursive build left subtree rch[root] = build (p + 1, R1, L2 + CNT, r2-1);//recursively build right subtree return root;} int best, best_sum;void dfs (int u, int sum)//First order traversal find optimal leaf {sum + = U;if (!lch[u] &&!rch[U]) {if (Sum < Best_sum | | (sum = = Best_sum&&u < best)) {best = U;best_sum = sum;}} if (Lch[u]) DFS (lch[u], sum), if (Rch[u]) DFS (rch[u], sum);} int main () {//freopen ("T.txt", "R", stdin), while (Read_list (In_order)) {read_list (Post_order); build (0, n-1, 0, n-1); BES T_sum = 100000000;dfs (post_order[n-1], 0); cout << best << Endl;} return 0;}

Example 6-8 Tree UVa548

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.