DFS  O(n) Time  O(logn)Space
Intuition\n Describe your first thoughts on how to solve this problem. \nDFS on all nodes to find possible Perfect Binary Tree, track their sizes\n# Approach\n 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDFS on all nodes to find possible Perfect Binary Tree, track their sizes\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse a dictionary to track the occurrences of different sizes. Sort them then find the kth largest\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n), n = number of nodes in root\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(logn)\n# Code\n```python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def kthLargestPerfectSubtree(self, root: Optional[TreeNode], k: int) -> int:\n        res = defaultdict(int)\n        def dfs(node):\n            if not node:\n                return 0\n            ls = dfs(node.left)\n            rs = dfs(node.right)\n\n            if ls == -1 or rs == -1 or ls != rs:\n                return -1\n            size = 1 + 2 * ls\n            res[size] += 1\n            return size\n        \n        dfs(root)\n\n        keys = sorted(res.keys())\n        cnt = 0\n        for key in keys[::-1]:\n            cnt += res[key]\n            if cnt >= k:\n                return key\n        else:\n            return -1\n```
['Python3']