bamboozled
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 */\n\n\n// easier way of seeing this \n\nclass Solution {\n    priority_queue<int, vector<int> , greater<int>> pq;  \npublic:\n    pair<bool,int> dfs(TreeNode* r,int k ) {\n            if (r == nullptr) {\n                return {true,0};\n            }\n            pair<bool,int> le = dfs(r->left, k);\n            pair<bool,int> ri = dfs(r->right, k );\n            // relations\n            if (le.first && ri.first && le.second == ri.second )  {\n                // perfect tree of size \n                pq.push(int(pow(2,ri.second + 1)) - 1);\n                if ((pq.size()) > k) {\n                    pq.pop();\n                }\n                return {true,le.second + 1};\n            }\n            return {false , -1};\n    }\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        dfs(root,k);\n        if (pq.size() != k) {\n            return -1;\n        }\n        return pq.top();\n    }\n};\n```
['C++']