Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Here's how you can approach this problem. It's easier to think of how to approach each step by looking from reverse:</h2> <pre><code> 8 / \ 4 9 / \ \ 2 6 10 / 1 </code></pre> <p>You have the following:</p> <p>Inorder: <code>1 2 4 6 8 9 10</code></p> <p>Level: <code>8 4 9 2 6 10 1</code></p> <h3>Level 1 - Root</h3> <p>A level traversal is a left to right, top to down traversal of the tree (like breadth-first search). In this example, you know that <code>8</code> will be the root node. Now looking at the inorder traversal, we know that <code>1 2 4 6</code> make up the left subtree and <code>9 10</code> make the right subtree. So we have:</p> <pre><code> 8 1 2 4 6 9 10 </code></pre> <p>While preserving order, create a copy of the level traversal without the nodes we are going to visit for the left and right recursive construction. Below notes will go through the left tree steps and what is passed through:</p> <h3>Level 2 - Left Subtree</h3> <p>Inorder: <code>1 2 4 6</code></p> <p>Level: <code>4 2 6 1</code></p> <ul> <li>Root: <code>4</code></li> <li>Left subtree: <code>1 2</code></li> <li>Right subtree: <code>6</code></li> </ul> <p>Result:</p> <pre><code> 8 / 9 10 4 2 1 \ 6 </code></pre> <h3>Level 3 - Left Subtree</h3> <p>Inorder: <code>1 2</code></p> <p>Level: <code>2 1</code></p> <ul> <li>Root: <code>2</code></li> <li>Left subtree: <code>1</code></li> <li>Right subtree: empty</li> </ul> <p>Result:</p> <pre><code> 8 / 9 10 4 / \ 2 6 / 1 </code></pre> <p>Now that we're done recursing all the way left, hopefully you can walk through on how to deal with the right children of the tree! Once you have the algorithm, you should be able to construct back the tree given two different traversals. The key is to recognize based on the two traversals how to determine the <strong>root</strong> at each recursive call, and the rest should follow through.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload