-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path106.py
More file actions
38 lines (29 loc) · 1.08 KB
/
106.py
File metadata and controls
38 lines (29 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution:
def __init__(self):
self.index = 0
def recoverFromPreorder(self, traversal: str) -> TreeNode:
return self.helper(traversal, 0)
def helper(self, traversal, depth):
if self.index >= len(traversal):
return None
dash_count = 0
while (
self.index + dash_count < len(traversal)
and traversal[self.index + dash_count] == "-"
):
dash_count += 1
# If the number of dashes doesn't match the current depth, return null
if dash_count != depth:
return None
self.index += dash_count
# Extract the node value
value = 0
while self.index < len(traversal) and traversal[self.index].isdigit():
value = value * 10 + int(traversal[self.index])
self.index += 1
# Create the current node
node = TreeNode(value)
# Recursively build the left and right subtrees
node.left = self.helper(traversal, depth + 1)
node.right = self.helper(traversal, depth + 1)
return node