Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:18:35+00:00 2026-05-31T13:18:35+00:00

I see that the code is posted here: How do I output the preorder

  • 0

I see that the code is posted here:
How do I output the preorder traversal of a tree given the inorder and postorder tranversal?

I have trouble understanding the logic though, especially, the recursion for the right part of the tree:

postorder(preorder, prestart+i-inostart+1, inorder, i+1, length-i+inostart-1);

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:18:36+00:00Added an answer on May 31, 2026 at 1:18 pm

    assume the binary expression tree and how the inorder, preorder and postorder traversals act on it:

    1. inorder: left subtree, current root, right subtree
    2. preorder: current root, left subtree, right subtree
    3. postorder: left subtree, right subtree, current root

    now the current code, first identifies the left and right subtrees. as we know the first element of preorder array is our root and we could find our corresponding root in inorder array. as we know all elements before root are left subtree’s elements and all elements after it are right subtree’s elements.

    we want to produce postorder traversal so we recursively continue with left subtree:

       postorder(preorder, prestart+1, inorder, inostart, i-inostart);
    
    1. prestart+1: because the root of left subtree is just after the current root in preorder traversal
    2. i-inostart: because i is the index of our root in inorder array so i-inostart would be the size of left subtree

    then right subtree:

       postorder(preorder, prestart+i-inostart+1, inorder, i+1, length-i+inostart-1);
    
    1. prestart+i-inostart+1: as I told above i-inostart is the size of left subtree and also we know that in preorder array the root of right subtree would come after the current root and the whole subtree so its index would be prestart+i-inostart+1
    2. i+1: because in inorder array the root’s index was i and the element after that should be the start of right subtree in inorder array
    3. length-i+inostart-1: because the size of right subtree would be length minus the size of left subtree and minus 1 (the root)

    and then output our current root:

       cout<<preorder[prestart]<<" ";
    

    doing so recursively would result in postorder traversal of the tree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that is using SyncEnumerator. As you can see here ,
I have an example of some code that I see often in websites that
how to see items that i pick on ListView ? i have this code:
I often see a pattern used in circumstances where we have look-up code that
Ok, I have code that is working (posted after this) but before I expand
if you can check in my code here, you can see that the umlauts
I need to do some connectivity simulations to see that my code handles various
When I debug code below, I see that span is changed by mapkit from
I`m reading the code of some open source project. I see that in one
Examining the compiled code of the sinf() on our GCC implementation, I see that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.