DFS to find perfect tree size
Intuition\n- DFS to find perfect tree size (-1 if not perfect)\n- Add to global vector of all perfect sizes\n- nth_element to retrieve result\n\n \n# Code\ncpp 
# Intuition\n- DFS to find perfect tree size (`-1` if not perfect)\n- Add to global vector of all perfect sizes\n- `nth_element` to retrieve result\n\n \n# Code\n```cpp []\nclass Solution {\n    vector<int> size;\n    int perfectSize(TreeNode* root) { // return -1 if not perfect tree\n        int sz = 0;\n        if(root) {\n            if(int l = perfectSize(root->left), r = perfectSize(root->right); l == r)\n                sz = l*2+1;\n            else\n                sz = -1;\n        }\n        if(sz > 0) \n            size.push_back(sz);\n        return sz;\n    }\npublic:\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        perfectSize(root);\n        if(k > size.size())\n            return -1;\n        auto kth = size.begin() + k - 1;\n        nth_element(size.begin(), kth, size.end(), greater<>());\n        return *kth;\n    }\n};\n```
['C++']