Create Binary Tree From Descriptions LeetCode solution in Python

1
class Solution:
2
def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
3
hashmap = {}
4
nodes = set()
5
children = set()
6
for parent, child, isLeft in descriptions:
7
nodes.add(parent)
8
nodes.add(child)
9
children.add(child)
10
if parent not in hashmap:
11
hashmap[parent] = TreeNode(parent)
12
if child not in hashmap:
13
hashmap[child] = TreeNode(child)
14
if isLeft:
15
hashmap[parent].left = hashmap[child]
16
if not isLeft:
17
hashmap[parent].right = hashmap[child]
18

19
for node in nodes:
20
if node not in children:
21
return hashmap[node]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0