Recover a Binary Tree from Preorder Values with Dash Depths

Quick Overview

Recover a Binary Tree from Preorder Values with Dash Depths. The string traversal records a valid preorder walk of a binary tree.

Recover a Binary Tree from Preorder Values with Dash Depths

Company: Grammarly

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem The string `traversal` records a valid preorder walk of a binary tree. Each node is written as D dashes followed by its positive decimal value, where D is its depth and root depth is0. A node with exactly one child always has a left child. Recover the tree and return its original TreeNode-style root representation. ```cpp class Solution { public: TreeNode* recoverFromPreorder(string traversal); }; ``` ### Examples **Example 1:** ```text Input: traversal = "8-4--2--6-12" Output: tree displayed as[8,4,12,2,6] Explanation: Depth-two values2 and6 attach to4; returning to depth one attaches12 as the root's right child. ``` **Example 2:** ```text Input: traversal = "5-3--1" Output: tree displayed as[5,3,null,1] Explanation: Both one-child nodes use left links, as guaranteed. ``` ### Constraints - The original tree contains1 to1000 nodes. - `1 <= Node.val <= 10^9`. - The string is its described preorder-depth output, with every sole child on the left. ### Contract considerations The input is a valid traversal under the specified depth and child rules. No extra input-length bound or raw-array replacement interface is introduced. ```hint Read the boundary condition The number of dashes tells you how far to pop the current ancestor stack. ``` ### Boundary reminders The input is a valid traversal under the specified depth and child rules. No extra input-length bound or raw-array replacement interface is introduced.

Overview: Recover a Binary Tree from Preorder Values with Dash Depths. The string traversal records a valid preorder walk of a binary tree.

Grammarly logo
Grammarly
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The string traversal records a valid preorder walk of a binary tree. Each node is written as D dashes followed by its positive decimal value, where D is its depth and root depth is0. A node with exactly one child always has a left child. Recover the tree and return its original TreeNode-style root representation.

class Solution {
public:
    TreeNode* recoverFromPreorder(string traversal);
};

Examples

Example 1:

Input: traversal = "8-4--2--6-12"
Output: tree displayed as[8,4,12,2,6]
Explanation: Depth-two values2 and6 attach to4; returning to depth one attaches12 as the root's right child.

Example 2:

Input: traversal = "5-3--1"
Output: tree displayed as[5,3,null,1]
Explanation: Both one-child nodes use left links, as guaranteed.

Constraints

  • The original tree contains1 to1000 nodes.
  • 1 <= Node.val <= 10^9 .
  • The string is its described preorder-depth output, with every sole child on the left.

Contract considerations

The input is a valid traversal under the specified depth and child rules. No extra input-length bound or raw-array replacement interface is introduced.

Boundary reminders

The input is a valid traversal under the specified depth and child rules. No extra input-length bound or raw-array replacement interface is introduced.