精華區beta Marginalman 關於我們 聯絡資訊
2196. Create Binary Tree From Descriptions ## 思路 用HashTable紀錄Node ## Complexity Time, Space: O(N) ## Code ```python class Solution: def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]: mapping = {} children = set() for parent, child, is_left in descriptions: children.add(child) if child not in mapping: mapping[child] = TreeNode(child) if parent not in mapping: mapping[parent] = TreeNode(parent) if is_left: mapping[parent].left = mapping[child] else: mapping[parent].right = mapping[child] for key, node in mapping.items(): if key not in children: return node return None ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.46 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1721013249.A.4CF.html