-
Notifications
You must be signed in to change notification settings - Fork 0
/
1028 Recover a Tree From Preorder Traversal.py
44 lines (42 loc) · 1.5 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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
def parseDigits(txt, i):
ret = []
while i<len(txt) and txt[i].isdigit():
ret.append(txt[i])
i += 1
return(int(''.join(ret)), i)
def parseMinus(txt, i):
ret = 0
while i<len(txt) and txt[i] == '-':
i += 1
ret += 1
return (ret, i)
ladder = []
if not traversal: return None
lastDepth = 0
i = 0
rootVal, i = parseDigits(traversal, i)
ladder.append(TreeNode(rootVal))
while i<len(traversal):
depth, i = parseMinus(traversal, i)
val, i = parseDigits(traversal, i)
# print("d", depth, " v", val)
if depth > lastDepth:
if len(ladder)-1 < depth:
ladder.append(TreeNode(val))
else:
ladder[depth] = TreeNode(val)
ladder[depth-1].left = ladder[depth]
#print(ladder[depth-1].val, " leftpoints to ", ladder[depth].val)
else:
ladder[depth] = TreeNode(val)
ladder[depth-1].right = ladder[depth]
lastDepth = depth
return ladder[0]