Python DFS + PQ solution, O(NlogK)
Code\npython3 []\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# 
# 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        q = []\n\n        # Returns perfect subtree size\n        def dfs(node):\n            if not node:\n                return 0\n\n            lperfect = dfs(node.left)\n            rperfect = dfs(node.right)\n\n            if lperfect == -1 or rperfect == -1 or lperfect != rperfect:\n                return -1\n\n            # Here lperfect == rperfect\n            perfect = 2 * lperfect + 1\n    \n            if len(q) < k:\n                heapq.heappush(q, perfect)\n            else:\n                heapq.heappushpop(q, perfect)\n\n            return perfect\n        \n        dfs(root)\n        return -1 if len(q) < k else  heapq.heappop(q)\n\n```
['Python3']