Simple Brute force Solution
Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n)\n\n# Code\ncpp []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     in
# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: 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 {\nprivate:\n    void checkPerfection(TreeNode* &root, vector<int> &v){\n        queue<TreeNode*> q;\n        q.push(root);\n        int perfectTreeSize = 0, height = 0;\n        while(!q.empty()){\n            int n = q.size();\n            // no need to proceed further if tree is not perfect\n            // since every row size should be power of 2 (if it\'s perfect)\n            if(n != pow(2, height)) return;\n            perfectTreeSize += n;\n            for(int i = 0; i < n; i++){\n                TreeNode* node = q.front();\n                q.pop();\n                if(node->left) q.push(node->left);\n                if(node->right) q.push(node->right);\n            }\n            height++;\n        }\n        v.push_back(perfectTreeSize); // push the size of perfect tree\n    }\n    void traverse(TreeNode* &root, vector<int> &v){\n        if(!root) return;\n        traverse(root->left, v);\n        checkPerfection(root, v);\n        traverse(root->right, v);\n    }\npublic:\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        vector<int> v;\n        traverse(root, v);\n        sort(v.begin(), v.end(), greater<>());\n        return k <= v.size() ? v[k-1] : -1;\n    }\n};\n```
['Tree', 'Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'C++']