C++ O(N) time O(1) space
Intuition\n1) A subtree is perfect if it\'s left & right subtrees are perfect and their sizes are equal\n1) Every size of a perfect subtree can be presented as 
# Intuition\n1) A subtree is perfect if it\'s left & right subtrees are perfect and their sizes are equal\n1) Every size of a perfect subtree can be presented as 2^x -1 (1 3 7 15 31 63 127 255 511 1023 2047) and since the max # of nodes is 2000, we can count the # of sizes of perfect trees using an array of size 10 (min pow of 2 covering 2000) and thus find the kth largest one\n\n- Time complexity: O(N)\n- Space complexity: O(1)\n\n# Code\n```cpp []\n    int CountPerfectTreesSizes(TreeNode* n, array<size_t, 10>& perfectTreesSizesCnt)\n    {\n        if (!n) return 0;\n\n        int l{ CountPerfectTreesSizes(n->left, perfectTreesSizesCnt) };\n        int r{ CountPerfectTreesSizes(n->right, perfectTreesSizesCnt) };\n\n        if (l == -1 || r == -1 || l != r)\n            return -1;\n\n        const int size{ l + r + 1 };\n        ++perfectTreesSizesCnt[log2(size + 1) - 1];\n        return size;\n    }\n\n    int kthLargestPerfectSubtree(TreeNode* root, int k) \n    {\n        array<size_t, 10> perfectTreesSizesCnt{};\n        CountPerfectTreesSizes(root, perfectTreesSizesCnt);\n\n        for (int i = perfectTreesSizesCnt.size() - 1; i >= 0; --i)\n        {\n            k -= perfectTreesSizesCnt[i];\n            if (k <= 0)\n                return pow(2, i + 1) - 1;\n        }\n\n        return -1;\n    }\n```
['C++']