-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path__main__.py
35 lines (27 loc) · 951 Bytes
/
__main__.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
from typing import List, Optional
# 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 createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
if not descriptions:
return None
ret = None
map_val_node = {val: TreeNode(val) for _, val, _ in descriptions}
for parent, val, isLeft in descriptions:
if parent not in map_val_node:
ret = TreeNode(parent)
map_val_node[parent] = ret
if isLeft:
map_val_node[parent].left = map_val_node[val]
else:
map_val_node[parent].right = map_val_node[val]
return ret
print(
Solution().createBinaryTree(
[[20, 15, 1], [20, 17, 0], [50, 20, 1], [50, 80, 0], [80, 19, 1]]
)
)