easy || recursion || dfs
IntuitionApproachComplexity
Time complexity:
o(n)
Space complexity:
o(h)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)$$ -->
o(n)

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

# 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>ans;
    int solve(TreeNode* root){
        if(!root) return 0;
        int left = solve(root->left);
        int right = solve(root->right);
        if(left == right){
            int total_a= 1+ 2*left;
            ans.push_back(total_a);
            return total_a;
        }
        return -1;
    }
    int kthLargestPerfectSubtree(TreeNode* root, int k) {
        solve(root);
        if(ans.size()<k) return -1;
        sort(ans.begin(),ans.end(),greater<int>());
        return ans[k-1];
    }
};
```
['C++']