Re: [闲聊] 每日leetcode

楼主: dont   2024-07-15 11:14:07
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
```

Links booklink

Contact Us: admin [ a t ] ucptt.com