classTreeNode(object):def__init__(self,x):self.val=xself.left=Noneself.right=NonefromcollectionsimportdequeclassCodec:defserialize(self,root):"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""ifnotroot:return"[]"ans=[]queue=deque()queue.append(root)whilequeue:# BFSnode=queue.popleft()ifnode:ans.append(str(node.val))queue.append(node.left)queue.append(node.right)else:ans.append("null")return'['+",".join(ans)+']'defdeserialize(self,data):"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""ifnotdataordata=="[]":returnNonedata=data.strip('[').strip(']').split(',')root=TreeNode(int(data[0]))queue=deque()queue.append(root)i=1whilequeue:node=queue.popleft()ifdata[i]!="null":# Check the left nodenode.left=TreeNode(int(data[i]))queue.append(node.left)i+=1ifdata[i]!="null":# Check the right nodenode.right=TreeNode(int(data[i]))queue.append(node.right)i+=1returnroot
classSolution:defpermutation(self,s:str)->List[str]:ans=set()# Avoid duplicatesvisited=[0for_inrange(len(s))]defback(path):iflen(path)==len(s):ans.add(path[:])returnforiinrange(0,len(s)):ifvisited[i]==0:visited[i]=1back(path+s[i])visited[i]=0# Go backback("")returnlist(ans)