Simple recursion solution || c++ || beats 100%
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> vals;
    int solve(TreeNode* root){
        if(!root){
            return 0;
        }

        int val1 = solve(root->left);
        int val2 = solve(root->right);

        if(val1 == -1 || val2 == -1){
            return -1;
        }
        if(val1 != val2){
            return -1;
        }
        vals.push_back(val1 + val2 + 1);
        return val1 + val2 + 1;
    }
    int kthLargestPerfectSubtree(TreeNode* root, int k) {
        int res = solve(root);

        sort(vals.begin(),vals.end());
        int n = vals.size();
        if( k > n ) return -1;
        return vals[n-k];
    }
};
```
['C++']