🐍 Python | Beats 98.46% | Easy-to-Read Solution 🐍
IntuitionBuild a folder tree (trie) from the paths.
Serialize each subtree into a unique string.
If two subtrees have the same serialization, they’re duplicates
# Intuition
Build a folder tree (trie) from the paths.
Serialize each subtree into a unique string.
If two subtrees have the same serialization, they’re duplicates.
Mark duplicate folders (and all their subfolders) for deletion.
Collect paths from the remaining tree.

# Approach
Build the Tree: Convert the list of paths into a nested dictionary structure.
Serialize Subtrees: Use DFS to create a string for each subtree (sorting children ensures consistency).
Mark Duplicates: For any serialization that appears more than once, mark those nodes for deletion.
Collect Remaining Paths: Traverse the tree to gather paths that are not marked for deletion.

# Complexity
Time: O(P × L × log(L)), where P is the number of paths and L is the average path length (due to sorting at each node).

Space: O(P × L) for the tree and recursion stack.

# Code
```python3 []
from collections import defaultdict
from typing import List

class Solution:
    def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:
        # Build the folder tree as a nested dictionary.
        tree = {}
        for path in paths:
            node = tree
            for folder in path:
                node = node.setdefault(folder, {})
        
        # Dictionary to map serialized subtree representations to the list of nodes with that structure.
        duplicates = defaultdict(list)
        
        # Serialize the folder tree using DFS.
        def serialize(node):
            # Base case: if the node is empty (no subfolders), return a fixed representation.
            if not node:
                return "()"
            # Process children in sorted order to ensure that identical structures produce the same serialization.
            children_serialization = "".join(child + serialize(child_node) for child, child_node in sorted(node.items()))
            # Wrap the serialization with parentheses.
            serial = "(" + children_serialization + ")"
            # Map this serialization to the current node.
            duplicates[serial].append(node)
            return serial
        
        # Serialize the whole tree to populate the 'duplicates' dictionary.
        serialize(tree)
        
        # Mark duplicate nodes (and their entire subtrees) for deletion.
        # If a serialized structure appears more than once, all corresponding nodes are marked.
        for nodes in duplicates.values():
            if len(nodes) > 1:
                for node in nodes:
                    node.clear()      # Remove all subfolders.
                    node["#"] = True  # Mark the node as deleted.
        
        # Collect the remaining folder paths (those not marked for deletion).
        ans = []
        def collect_paths(node, path):
            # Iterate over children without creating an extra list copy.
            for child_name, child_node in node.items():
                # Skip nodes that have been marked for deletion.
                if "#" in child_node:
                    continue
                new_path = path + [child_name]
                ans.append(new_path)
                collect_paths(child_node, new_path)
        
        collect_paths(tree, [])
        return ans

```
['Python3']