Easy Solution
Code
# 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:
int c , mx;
bool isPowerOfTwo(int x) {
    return (x != 0) && ((x & (x - 1)) == 0);
}
void solve(TreeNode*root,vector<int>&ans,int l) {
    if (root==NULL) {
        mx = max (mx , l);
        return;
    }
    int y = mx;
    c++;
    int d = c;
    solve(root->left,ans,l+1);
    int t = mx;
    mx = y;
    solve(root->right,ans,l+1);
    mx = max (mx , t);
    if (root->val == 11) {
        cout << mx - l << " assdd " << (int)log2(c-d+1) + 1 <<  endl;
    }
    if (isPowerOfTwo(c - d + 1 + 1) && mx - l == (int)log2(c-d+1) + 1) {
        ans.push_back(c - d + 1);
    }
}
    int kthLargestPerfectSubtree(TreeNode* root, int k) {
        c = 0;
        mx = 0;
        vector<int>ans;
        solve(root,ans,1);
        sort(ans.rbegin(),ans.rend());
        for (int i : ans) cout << i << " ";
        cout << endl;
        if (ans.size() < k) return -1;
        return ans[k-1];
    }
};
```
![download.jpeg](https://assets.leetcode.com/users/images/3ea45e4d-f367-43c9-b94e-bf385bdcd79b_1743719933.341657.jpeg)

['Depth-First Search', 'Sorting', 'C++']