C++ solution , 3ms Beats 94.92%
Intuition\n Describe your first thoughts on how to solve this problem. \nThis problem is same as balanced binary checker Leetcode 110.\n\n# Approach\n Describe 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis problem is same as balanced binary checker Leetcode 110.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n) vector size and O(n) stack space\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),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n    vector<int> ans;\n    int check(TreeNode* root) {\n        if (root == nullptr) {\n            return 0;\n        }\n        int l = check(root->left);\n        // if (l == -1)\n        // return -1;\n\n        int r = check(root->right);\n        // if (r == -1)\n        // return -1;\n\n        if(l == -1 || r == -1)\n            return -1;\n\n        if (l != r)\n            return -1;\n        if (l == r && l != -1 && r != -1) {\n            ans.push_back(pow(2, l + 1) - 1);\n        }\n\n        return 1 + max(l, r);\n    }\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        check(root);\n        sort(ans.begin(), ans.end(), greater<int>());\n\n        // cout << ans[0];\n\n        //for (int x : ans) {\n            //cout << x << endl;\n        //}\n\n        if (ans.size() >= k)\n            return ans[k - 1];\n        else\n            return -1;\n    }\n};\n```
['C++']