Create Binary Tree From Descriptions LeetCode solution in Python
1class Solution:2def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:3hashmap = {}4nodes = set()5children = set()6for parent, child, isLeft in descriptions:7nodes.add(parent)8nodes.add(child)9children.add(child)10if parent not in hashmap:11hashmap[parent] = TreeNode(parent)12if child not in hashmap:13hashmap[child] = TreeNode(child)14if isLeft:15hashmap[parent].left = hashmap[child]16if not isLeft:17hashmap[parent].right = hashmap[child]1819for node in nodes:20if node not in children:21return hashmap[node]