Easy C++ Solution ✔️✔️
\n# Complexity\n- Time complexity: O(n)\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
\n# Complexity\n- Time complexity: $$O(n)$$\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    pair<int,int> dfs(TreeNode* root,vector<int>& vec){\n        if (root==NULL)return {0,1};\n        pair<int,int> lft= dfs(root->left,vec);\n        pair<int,int> rgt= dfs(root->right,vec);\n        bool flag=0;\n        if ((lft.first==rgt.first) && (lft.second==1) && (rgt.second==1) && \n        ((root->left && root->right)||(root->left==NULL && root->right==NULL))){\n            flag= 1;\n            vec.push_back((1<<(lft.first+1))-1);\n        }\n        return {max(lft.first,rgt.first)+1,flag};\n    }\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        vector<int> vec;\n        dfs(root,vec);\n        sort(vec.begin(),vec.end(),greater<int>());\n        if (vec.size()<k)return -1;\n        return vec[k-1];\n    }\n};\n```
['C++']