Easy Pthon solution using dfs
IntuitionApproachComplexity
Time complexity:O(N log N) (due to sorting)

Space complexity:O(N) (due to storing perfect subtree sizes)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:O(N log N) (due to sorting)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:O(N) (due to storing perfect subtree sizes)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python []
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def kthLargestPerfectSubtree(self, root, k):
        """
        :type root: Optional[TreeNode]
        :type k: int
        :rtype: int
        """
        perfect_tree_size = []
        def calc(node):
            if not node:
                return [True, 0]
            
            is_left, left_height = calc(node.left)
            is_right, right_height = calc(node.right)
            if not is_left or not is_right or left_height != right_height:
                return [False, -1]
            perfect_tree_size.append(2 ** (left_height + 1) - 1)
            return [True, left_height + 1]
        calc(root)

        perfect_tree_size.sort(reverse = True)
        print(perfect_tree_size)
        return perfect_tree_size[k - 1] if k <= len(perfect_tree_size) else -1






```
['Depth-First Search', 'Sorting', 'Python']