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  # @Title: 根据描述创建二叉树 (Create Binary Tree From Descriptions) # @Author: 15816537946@163.com # @Date: 2022-03-06 14:13:11 # @Runtime: 616 ms # @Memory: 22.6 MB class Solution: def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]: v = dict() deg = collections.Counter() for p, c, left in descriptions: if p not in v: v[p] = TreeNode(p) if c not in v: v[c] = TreeNode(c) if left == 1: v[p].left = v[c] else: v[p].right = v[c] deg[c] += 1 for i in v: if deg[i] == 0: return v[i] return None