Skip to content

Commit c3e9d33

Browse files
committed
Runtime: 178 ms (Top 20.74%) | Memory: 14.8 MB (Top 52.51%)
1 parent 1e7e17d commit c3e9d33

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Runtime: 178 ms (Top 20.74%) | Memory: 14.8 MB (Top 52.51%)
2+
13
class Solution:
24
def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
35
i = 0
@@ -7,10 +9,10 @@ def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
79
if traversal[i].isdigit():
810
value, i = get_value(traversal, i)
911
insert_node(dummy_head, depth, value)
10-
12+
1113
else:
1214
depth, i = get_depth(traversal, i)
13-
15+
1416
return dummy_head.left
1517

1618
# Returns the next value from the string traversal, and returns the position following the last digit of the current value.
@@ -20,7 +22,7 @@ def get_value(traversal, i):
2022
value *= 10
2123
value += int(traversal[i])
2224
i += 1
23-
25+
2426
return value, i
2527

2628
# Insertes a node of the given `value` at the given `depth` of the subtree whose root is the given `root`.
@@ -30,18 +32,18 @@ def insert_node(root, depth, value):
3032
root = root.right
3133
else:
3234
root = root.left
33-
35+
3436
new_node = TreeNode(value)
3537
if root.left:
3638
root.right = new_node
3739
else:
3840
root.left = new_node
39-
41+
4042
# Gets the next depth from the string traversal, and returns the position following the last dash of the current depth.
4143
def get_depth(traversal, i):
4244
depth = 0
4345
while i < len(traversal) and traversal[i] == "-":
4446
depth += 1
4547
i += 1
46-
47-
return depth, i
48+
49+
return depth, i

0 commit comments

Comments
 (0)