-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1028 Recover a Tree From Preorder Traversal.py
143 lines (116 loc) · 3.47 KB
/
1028 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python3
"""
We run a preorder depth first search on the root of a binary tree.
At each node in this traversal, we output D dashes (where D is the depth of this
node), then we output the value of this node. (If the depth of a node is D, the
depth of its immediate child is D+1. The depth of the root node is 0.)
If a node has only one child, that child is guaranteed to be the left child.
Given the output S of this traversal, recover the tree and return its root.
Example 1:
Input: "1-2--3--4-5--6--7"
Output: [1,2,5,3,4,6,7]
Example 2:
Input: "1-2--3---4-5--6---7"
Output: [1,2,5,3,null,6,null,4,null,7]
Example 3:
Input: "1-401--349---90--88"
Output: [1,401,null,349,88,90]
Note:
The number of nodes in the original tree is between 1 and 1000.
Each node will have a value between 1 and 10^9.
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
from collections import OrderedDict
class Solution:
def recoverFromPreorder(self, S: str) -> TreeNode:
"""
map: node -> depth
stack of pi (incompleted)
"""
depth = 0
# parse
n = len(S)
i = 0
root = None
stk = []
while i < n:
if S[i] == "-":
depth += 1
i += 1
else:
j = i
while j < n and S[j] != "-":
j += 1
val = int(S[i:j])
# construct
cur = TreeNode(val)
if depth == 0:
root = cur
stk = [(depth, root)]
else:
assert stk
while stk[-1][0] != depth - 1:
stk.pop()
_, pi = stk[-1]
if not pi.left:
pi.left = cur
elif not pi.right:
pi.right = cur
stk.pop()
else:
raise
stk.append((depth, cur))
depth = 0
i = j
return root
def recoverFromPreorder_error(self, S: str) -> TreeNode:
"""
map: node -> depth
stack of pi (incompleted)
"""
depth = 0
depths = OrderedDict()
# parse
n = len(S)
i = 0
while i < n:
if S[i] == "-":
depth += 1
i += 1
else:
j = i
while j < n and S[j] != "-":
j += 1
val = int(S[i:j])
depths[val] = depth
depth = 0
i = j
# construct
stk = []
root = None
for k, v in depths.items():
cur = TreeNode(k)
if v == 0:
root = cur
stk = [root]
else:
assert stk
while depths[stk[-1].val] != v - 1:
stk.pop()
if not stk[-1].left:
stk[-1].left = cur
elif not stk[-1].right:
stk[-1].right = cur
stk.pop()
else:
raise
stk.append(cur)
return root
if __name__ == "__main__":
assert Solution().recoverFromPreorder("5-4--4")
assert Solution().recoverFromPreorder("1-2--3--4-5--6--7")