Python, try all subsets, test if subset is subtree, find diameter of subtree
Use dfs to determine whether or not we are a valid subtree. Test connectivity(can reach all), and exclusivity(no extra nodes). To find diameter of tree, run dfs
Use dfs to determine whether or not we are a valid subtree. Test connectivity(can reach all), and exclusivity(no extra nodes). To find diameter of tree, run dfs twice. Since n is small enough can just use a bitmask to enumerate all possible subsets. Tougher to solve if n can grow large. \n```\nclass Solution:\n    def countSubgraphsForEachDiameter(self, n: int, edges: List[List[int]]) -> List[int]:\n        def dfs(edges,start,i): # returns whether or not we are a tree\n            seen = set()\n            stack = [start]\n            while len(stack) > 0:\n                curr = stack.pop()\n                if curr not in seen:\n                    seen.add(curr)\n                    for neigh in edges[curr]:\n                        stack.append(neigh)\n            nodeSet = set()\n            for j in range(n):\n                if i & (1 << j):\n                    nodeSet.add(j)\n            return nodeSet == seen\n        \n        def diameter(edges,start): # returns the diameter of tree\n            farthest = start\n            farthestDist = 0\n            seen = set()\n            stack = [(start,0)]\n            while len(stack) > 0:\n                curr,dist = stack.pop()\n                if curr not in seen:\n                    seen.add(curr)\n                    if dist > farthestDist:\n                        farthest = curr\n                        farthestDist = dist\n                    for neigh in edges[curr]:\n                        stack.append((neigh,dist+1))\n            start = farthest\n            farthestDist = 0\n            seen = set()\n            stack = [(start,0)]\n            while len(stack) > 0:\n                curr,dist = stack.pop()\n                if curr not in seen:\n                    seen.add(curr)\n                    if dist > farthestDist:\n                        farthest = curr\n                        farthestDist = dist\n                    for neigh in edges[curr]:\n                        stack.append((neigh,dist+1))\n            return farthestDist\n            \n        diameterCounts = defaultdict(int)\n        for i in range(1,2**n):\n            if sum([int(elem) for elem in bin(i)[2:]]) == 1:\n                continue\n            subTreeEdges = defaultdict(list)\n            for edge in edges:\n                l,m = edge\n                l -= 1\n                m -= 1\n                if i & (1 << l) and i & (1 << m):\n                    subTreeEdges[l].append(m)\n                    subTreeEdges[m].append(l)\n            start = None\n            for j in range(n):\n                if i & (1 << j):\n                    start = j\n                    break\n            isSubtree = dfs(subTreeEdges,start,i)\n            if isSubtree:\n                diameterCounts[diameter(subTreeEdges,start)] += 1\n            else:\n                pass\n        \n        return [diameterCounts[i] for i in range(1,n)]            \n```
[]