[Python3] serialize sub-trees
\n\nclass Solution:\n    def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:\n        paths.sort()\n        \n        tree = {"#": -1}\n
\n```\nclass Solution:\n    def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:\n        paths.sort()\n        \n        tree = {"#": -1}\n        for i, path in enumerate(paths): \n            node = tree\n            for x in path: node = node.setdefault(x, {})\n            node["#"] = i\n        \n        seen = {}\n        mark = set()\n        \n        def fn(n): \n            """Return serialized value of sub-tree rooted at n."""\n            if len(n) == 1: return "$" # leaf node \n            vals = []\n            for k in n: \n                if k != "#": vals.append(f"${k}${fn(n[k])}")\n            hs = "".join(vals)\n            if hs in seen: \n                mark.add(n["#"])\n                mark.add(seen[hs])\n            if hs != "$": seen[hs] = n["#"]\n            return hs\n        \n        fn(tree)\n        \n        ans = []\n        stack = [tree]\n        while stack: \n            n = stack.pop()\n            if n["#"] >= 0: ans.append(paths[n["#"]])\n            for k in n: \n                if k != "#" and n[k]["#"] not in mark: stack.append(n[k])\n        return ans \n```
['Python3']