[Hiho 10] sequential traversal by sequence traversal

Source: Internet
Author: User

Title Description

Divide and conquer the thought, the recursive solution.

First achievement and then post-traversal:

#include <iostream> #include <algorithm> #include <cstring>using namespace Std;char pre[26], mid[26]; Typedefstruct _tree {char c;_tree *lc, *rc;_tree (char ch) {c = ch;}} Tree, *ptree; PTree maketree (int pb, int mb, int len) {if (len <= 0) return Nullptr;char Root_char = Pre[pb];int idx;for (idx = MB; i DX < MB + len; idx++) {if (mid[idx] = = Root_char) {break;}} PTree root = new Tree (Root_char), ROOT-&GT;LC = Maketree (pb + 1, MB, IDX-MB), ROOT-&GT;RC = Maketree (pb + 1 + idx-mb, ID x + 1, MB + len-idx-1); return root;} void Post_traverse (PTree root) {if (root = nullptr) return;post_traverse (ROOT-&GT;LC);p ost_traverse (ROOT-&GT;RC); cout << root->c;} int main () {CIN >> pre >> Mid;int len = strlen (pre); PTree root = maketree (0, 0, len);p ost_traverse (root); return 0;}

In fact, the process can be omitted, the processing of direct output after the post-traversal results:

#include <iostream> #include <algorithm> #include <cstring>using namespace Std;char pre[26], mid[26];  void Solve (int pb, int mb, int len) {if (len <= 0) Return;char Root_char = Pre[pb];int idx;for (idx = MB; idx < MB + Len idx++) {if (mid[idx] = = Root_char) {break;}} Solve (PB + 1, MB, IDX-MB), solve (PB + 1 + idx-mb, idx + 1, MB + len-idx-1); cout << Root_char;} int main () {CIN >> pre >> Mid;int len = strlen (pre); solve (0, 0, Len); return 0;}

[Hiho 10] sequential traversal by sequence traversal

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.