Post Order Traversal + PQ
Complexity\n- Time complexity: O(n + nlogk)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n + k)\n Add your space complexity here, e.g. O
# Complexity\n- Time complexity: $$O(n + nlogk)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n + k)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python []\n# Definition for a binary tree node.\n# class TreeNode(object):\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution(object):\n    def kthLargestPerfectSubtree(self, root, k):\n        """\n        :type root: Optional[TreeNode]\n        :type k: int\n        :rtype: int\n        """\n\n        heap = []\n\n        def dfs(node):\n            cur_size = 0\n            isPerfect = False\n            if not node.left and not node.right:\n                isPerfect = True\n                cur_size = 1\n            elif not node.left and node.right:\n                isPerfect = False\n                dfs(node.right)\n            elif not node.right and node.left:\n                isPerfect = False\n                dfs(node.left)\n            else:\n                l_status, l_sz = dfs(node.left)\n                r_status, r_sz = dfs(node.right)\n                if l_status and r_status and l_sz == r_sz:\n                    isPerfect = True\n                    cur_size = l_sz + r_sz + 1\n            \n            if isPerfect:\n                heapq.heappush(heap,cur_size)\n                if len(heap) > k:\n                    heapq.heappop(heap)\n            \n            return isPerfect,cur_size\n        \n        dfs(root)\n\n        if len(heap) == k:\n            return heap[0]\n        \n        return -1\n\n            \n\n        \n```
['Depth-First Search', 'Heap (Priority Queue)', 'Python']