Recover From Preorder Traversal — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Recover From Preorder Traversal
Detailed explanation and reference materials
Problem Overview

Recover From Preorder Traversal

Problem Statement

You are given a string representing the preorder depth-first search (DFS) traversal of a binary tree. In this traversal, each node is output as a series of dashes followed by its integer value. The number of dashes indicates the node's depth in the tree:

  • The root node (depth 0) is printed without any dashes.
  • For any node at depth D, its children are printed with D+1 leading dashes.
  • Note: If a node has only one child, it is guaranteed to be the left child.

Your task is to reconstruct the original binary tree from this traversal string and return the tree's root.


Task

Implement a function that, given the preorder traversal string, recovers the binary tree and returns its root.


Test Cases

Example 1

Input:
traversal = "10-20--30--40-50--60--70" graph Output:
[10, 20, 50, 30, 40, 60, 70]

Explanation:

  • The root is 10.
  • 20 is the left child of 10.
  • 30 and 40 are the left and right children of 20, respectively.
  • 50 is the right child of 10.
  • 60 and 70 are the left and right children of 50, respectively.

Example 2

Input:
traversal = "8-9--10---11-12--13---14" [8, 9, 12, 10, null, 13, null, 11, null, 14] graph Explanation:

  • The root is 8.
  • 9 is the left child of 8.
  • 10 is the left child of 9.
  • 11 is the left child of 10.
  • 12 is the right child of 8.
  • 13 is the left child of 12.
  • 14 is the left child of 13.

Example 3

Input:
traversal = "100-200--300---400---500-600--700" graph Output: [100, 200, 600, 300, null, 700, null, 400, 500]

Explanation:

  • The root is 100.
  • 200 is the left child of 100.
  • 300 is the left child of 200.
  • 400 and 500 are the left and right children of 300, respectively.
  • 600 is the right child of 100.
  • 700 is the left child of 600.

Constraints

  • The number of nodes in the original tree is in the range [1, 1000].
  • 1 <= Node.val <= 10⁹

Approach

  1. Parse the Traversal String:
    • Read the string sequentially.
    • Count the number of consecutive dashes to determine the depth of the current node.
    • Parse the following digits to form the node's value.
  2. Construct the Tree:
    • Use a stack to maintain nodes along the current path.
    • For each node encountered, compare its depth with the depth of the node on the top of the stack.
    • If the current node’s depth is greater, it is the left child of the previous node; otherwise, backtrack in the stack to find the correct parent and attach it as a right child.
  3. Return the Root:
    • After processing the entire string, the first element in the stack (or stored separately) will be the root of the reconstructed binary tree.

— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
leetcode-problem-of-the-day
binary-trees
java
Reference Link
https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/description

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.