Know the pre-order and Middle-order print post-order sequences of Binary Trees

Source: Internet
Author: User

Question: Know the Pre-and mid-order series of a binary tree and print its post-order series.
The time complexity of the following algorithm: O (n ^ 2) -- the worst case is the left single decision tree.

/* Kown the preorder listing and the inorder listing of a tree, print it's postorder listing */void PrintPostOrder (const char * pre, int s1, int e1, const char * in, int s2, int e2) {if (s1> e1 | s2> e2) return; char root = pre [s1]; for (int I = s2; I <= e2; I ++) if (in [I] = root) break; int offset = i-s2; PrintPostOrder (pre, s1 + 1, s1 + offset, in, s2, I-1); PrintPostOrder (pre, s1 + offset + 1, e1, in, I + 1, e2); printf ("% c", root );} int main () {const char * pre = "ABCDEF"; const char * in = "FEDCBA"; printf ("PreOrder: % s \ n", pre ); printf ("InOrder: % s \ n", in); printf ("PostOrder:"); PrintPostOrder (pre, 0, strlen (pre)-1, in, 0, strlen (in)-1); printf ("\ n"); 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.