C++ solution using recursion
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N log M),where N is the number of nodes and M is the no. of perfect binary tree.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N) \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int solve(TreeNode* root, priority_queue<int>& maxH) {\n        if (!root) return 0;  // Null node\n\n        // If it\'s a leaf node, count it as a perfect subtree of size 1\n        if (!root->left && !root->right) {\n            maxH.push(1);\n            return 1;\n        }\n\n        int left = solve(root->left, maxH);\n        int right = solve(root->right, maxH);\n\n        // If both subtrees are perfect and of equal size, this is a perfect subtree.\n        if (left != 0 && left == right) {\n            int size = 1 + left + right;\n            maxH.push(size);\n            return size;\n        }\n\n        // Not a perfect binary subtree\n        return 0;\n    }\n\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        priority_queue<int> maxH;\n        solve(root, maxH);\n\n        if (maxH.size() < k) return -1;  // Fewer than k perfect subtrees\n\n        for (int i = 1; i < k; i++) {\n            maxH.pop();\n        }\n\n        return maxH.top();\n    }\n};\n\n```
['C++']