forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover-a-tree-from-preorder-traversal.py
66 lines (60 loc) · 1.7 KB
/
recover-a-tree-from-preorder-traversal.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Time: O(n)
# Space: O(h)
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# iterative stack solution
class Solution(object):
def recoverFromPreorder(self, S):
"""
:type S: str
:rtype: TreeNode
"""
i = 0
stack = []
while i < len(S):
level = 0
while i < len(S) and S[i] == '-':
level += 1
i += 1
while len(stack) > level:
stack.pop()
val = []
while i < len(S) and S[i] != '-':
val.append(S[i])
i += 1
node = TreeNode(int("".join(val)))
if stack:
if stack[-1].left is None:
stack[-1].left = node
else:
stack[-1].right = node
stack.append(node)
return stack[0]
# Time: O(n)
# Space: O(h)
# recursive solution
class Solution2(object):
def recoverFromPreorder(self, S):
"""
:type S: str
:rtype: TreeNode
"""
def recoverFromPreorderHelper(S, level, i):
j = i[0]
while j < len(S) and S[j] == '-':
j += 1
if level != j - i[0]:
return None
i[0] = j
while j < len(S) and S[j] != '-':
j += 1
node = TreeNode(int(S[i[0]:j]))
i[0] = j
node.left = recoverFromPreorderHelper(S, level+1, i)
node.right = recoverFromPreorderHelper(S, level+1, i)
return node
return recoverFromPreorderHelper(S, 0, [0])