-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsolution.py
More file actions
26 lines (21 loc) · 801 Bytes
/
solution.py
File metadata and controls
26 lines (21 loc) · 801 Bytes
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
from typing import List, Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
self.preorder_index = 0
tree_map = {num: i for i, num in enumerate(inorder)}
def construct(left, right):
if left > right:
return None
val = preorder[self.preorder_index]
mid = tree_map[val]
self.preorder_index += 1
node = TreeNode(val)
node.left = construct(left, mid-1)
node.right = construct(mid+1, right)
return node
return construct(0, len(preorder)-1)