C++ || DFS and Sort
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 {\n    int getHeight(TreeNode* root, vector<int>& arr);\npublic:\n    int kthLargestPerfectSubtree(TreeNode* root, int k);\n};\n\nint Solution::getHeight(TreeNode* root, vector<int>& arr) {\n    if (!root) return 0; \n\n    const auto left = getHeight(root->left, arr);\n    const auto right = getHeight(root->right, arr);\n    \n    if (left == right && left >= 0) {\n        int height = left + 1;\n        int size = (1 << height) - 1;\n        // save total no of nodes till that height of perfect binary tree\n        arr.push_back(size); \n        return height; \n    }\n\n    return -1; \n}\n\nint Solution::kthLargestPerfectSubtree(TreeNode* root, int k) {\n    vector<int> res;\n    (void)getHeight(root, res);\n\n    sort(res.rbegin(), res.rend());\n    if (res.size() < k) \n        return -1;\n\n    return res[k - 1];\n}\n```
['C++']