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

  • SEARCH
  • Home
  • 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 4019172
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:04:22+00:00 2026-05-20T10:04:22+00:00

Im doing an assignment on building a binary tree from the preorder and inorder

  • 0

Im doing an assignment on building a binary tree from the preorder and inorder traversals (a char in each Node) and im trying to wrap my brain around how to build the actual tree.

Here is my thought process on how to accomplish this:

  1. store the first entry in the preorder as the root node
  2. search the inorder for that entry.
  3. take the chars to the left of the root node and save them as a char array.
  4. take the chars to the right of the root node and save them as a char array.
  5. make a new tree, with the root as the parent and its 2 children being the left and right char arrays.
  6. keep going recursively until the preorder length is 0.

I have steps 1-4 taken care of, but im not too sure how to properly build my tree, and was wondering if anyone had any pointers. Thank you.

  • 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-20T10:04:22+00:00Added an answer on May 20, 2026 at 10:04 am

    Do the recursion before building the new tree. So, your list would look like this:

    1. If the arrays have length 1, just return a leaf node with this single item in it. (This is the recursion foundation.) (O(1))
    2. Store the first entry in the preorder array as the root node. (O(1))
    3. Search the inorder array for that entry. (O(n))
    4. Take the chars to the left of the root node in the inorder array and save them as a char array. Take the same amount of characters from the preorder array (after the root). (O(n), or O(1) when just throwing pointers/indexes around.)
    5. Take the chars to the right of the root node and save them as a char array. Take the same amount of characters from the preorder array (after the first part – that should be just the remainding part). (O(n), or O(1) when just throwing pointers/indexes around.)
    6. Recursively make a tree from both left char arrays.
    7. Recursively make a tree from both right char arrays.
    8. Combine both trees with your root node. (O(1).)

    The non-recursive parts can be done in O(n) overall, and summing them up for each recursion level is also O(n) each. The total runtime thus depends on the number of recursion levels. If you have a approximately balanced tree, the depth is O(log n), thus we get to O(n · log n) at all. As the only necessarily slow part is the search of the root node in the inorder array, I guess we could optimize that even a bit more if we know more about the tree.

    In the worst case we have one recursion level for each node in the tree, coming to complexity O(n·n).

    Example: Preorder ABCDEF, Inorder FEDCBA, Tree:

                                       +---+
                                       | A |
                                       ++--+
                                        |
                                +---+   |
                                | B +<--+
                                ++--+
                                 |
                         +---+   |
                         | C +<--+
                         ++--+
                          |
                  +---+   |
                  | D +<--+
                  ++--+
                   |
           +---+   |
           | E +<--+
           ++--+
            |
    +---+   |
    | F +<--+
    +---+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way of doing parallel assignment in C++? Currently, the below compiles
I'm doing a basic homework assignment which looks like this: While input <> -1
When doing a simple performance measurement, I was astonished to see that calling String.IndexOf(char)
I'm building a thesaurus using a HashMap to store the synonyms. I'm trying to
I am doing my assignment in Network architecture 1, where I have to implement
I'm doing an assignment that requires me to complete the following block of code:
I'm doing an assignment about planning for tower of Hanoi problem using linear planning
Doing odd/even styling with jQuery is pretty easy: $(function() { $(.oddeven tbody tr:odd).addClass(odd); $(.oddeven
When doing an INSERT with a lot of data, ie: INSERT INTO table (mediumtext_field)
When doing small icons, header graphics and the like for websites, is it better

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.