2
def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:4
dummy_head = TreeNode()6
while i < len(traversal):7
if traversal[i].isdigit():8
value, i = get_value(traversal, i)9
insert_node(dummy_head, depth, value)12
depth, i = get_depth(traversal, i)14
return dummy_head.left17
# Returns the next value from the string traversal, and returns the position following the last digit of the current value.18
def get_value(traversal, i):20
while i < len(traversal) and traversal[i].isdigit():22
value += int(traversal[i])28
# Insertes a node of the given `value` at the given `depth` of the subtree whose root is the given `root`.29
def insert_node(root, depth, value):30
for _ in range(depth):36
new_node = TreeNode(value)43
# Gets the next depth from the string traversal, and returns the position following the last dash of the current depth.44
def get_depth(traversal, i):46
while i < len(traversal) and traversal[i] == "-":