Best Solution || Beats 100% Users || DFS
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:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\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    pair<int, bool> check(TreeNode* root, vector<int>& arr) {\n        if (!root) return {0, true}; \n        \n        auto left = check(root->left, arr);\n        auto right = check(root->right, arr);\n        \n        if (left.second && right.second && left.first == right.first) {\n            int height = left.first + 1;\n            int size = (1 << height) - 1;\n            arr.push_back(size); \n            return {height, true}; \n        }\n\n        return {0, false}; \n    }\n\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        vector<int> arr;\n\n        check(root, arr);\n\n        sort(arr.rbegin(), arr.rend());\n\n        if (arr.size() < k) return -1;\n\n        return arr[k - 1]; \n    }\n};\n```
['C++']