<p>We run a&nbsp;preorder&nbsp;depth-first search (DFS) on the <code>root</code> of a binary tree.</p>

<p>At each node in this traversal, we output <code>D</code> dashes (where <code>D</code> is the depth of this node), then we output the value of this node.&nbsp; If the depth of a node is <code>D</code>, the depth of its immediate child is <code>D + 1</code>.&nbsp; The depth of the <code>root</code> node is <code>0</code>.</p>

<p>If a node has only one child, that child is guaranteed to be <strong>the left child</strong>.</p>

<p>Given the output <code>traversal</code> of this traversal, recover the tree and return <em>its</em> <code>root</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/10/recover_tree_ex1.png" style="width: 423px; height: 200px;" />
<pre>
<strong>Input:</strong> traversal = &quot;1-2--3--4-5--6--7&quot;
<strong>Output:</strong> [1,2,5,3,4,6,7]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/10/recover_tree_ex2.png" style="width: 432px; height: 250px;" />
<pre>
<strong>Input:</strong> traversal = &quot;1-2--3---4-5--6---7&quot;
<strong>Output:</strong> [1,2,5,3,null,6,null,4,null,7]
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/10/recover_tree_ex3.png" style="width: 305px; height: 250px;" />
<pre>
<strong>Input:</strong> traversal = &quot;1-401--349---90--88&quot;
<strong>Output:</strong> [1,401,null,349,88,90]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the original tree is in the range <code>[1, 1000]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
</ul>

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
        # use a stack to go back up to a node
        # add to stack when traversing down
        # use dashes to determine if we're pushing a child onto stack,
        # or if we have to go back up (level of stack determines expected dashes?)
        # consisting of node vals and dash sequences
        vals = []
        curr = 0
        val = ""
        while curr < len(traversal):
            if traversal[curr] == "-":
                # dump whatever string we had
                vals.append((False, val))
                val = ""
                # go until we're not on a - anymore
                dashSpree = 0
                while traversal[curr] == "-" and curr < len(traversal):
                    dashSpree += 1
                    curr += 1
                vals.append((True, dashSpree))
            val += traversal[curr]
            curr += 1
        if val:
            vals.append((False, val))

        st = deque([])
        root = None
        for isSkip, val in vals:
            if isSkip:
                # print("level", val)
                currLevel = len(st)
                readLevel = val
                if readLevel == currLevel:
                    # do nothing.
                    continue
                elif readLevel < currLevel:
                    # we have to go back up
                    for i in range(readLevel, currLevel):
                        st.pop()
                elif readLevel > currLevel:
                    # we're going down???
                    raise "LMAO"

            else:
                # print("NODE", val)
                newNode = TreeNode(val)
                if not root:
                    root = newNode
                if st and st[-1]:
                    if st[-1].left is None:
                        st[-1].left = newNode
                    else:
                        st[-1].right = newNode
                st.append(newNode)

        return root
17500