Recursive Solution
Complexity
Time complexity:O(nLog(n))

Space complexity:O(n) (For recurvice stack)
Code
# Complexity
- Time complexity:$$O(nLog(n))$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:$$O(n)$$ (For recurvice stack)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def find(self, root, ans):
        if not root:
            return 0, True
        rCount, rPerfect = self.find(root.right, ans)
        lCount, lPerfect = self.find(root.left, ans)

        if rCount == lCount and rPerfect and lPerfect:
            ans.append(rCount+lCount+1)
            return rCount+lCount+1, True
        
        return -1, False

    def kthLargestPerfectSubtree(self, root: Optional[TreeNode], k: int) -> int:
        ans = []
        self.find(root, ans)
        if len(ans) < k:
            return -1

        ans.sort(reverse = True)
        # print(ans)

        return ans[k-1]
        
```
['Python3']