C++ || Easy to follow || First thought
ApproachDivided the code into three functions:
To check if the subtree is a perfect Binary Tree
Calculate the height of the tree such that all leaves are on the
# Approach
<!-- Describe your approach to solving the problem. -->
Divided the code into three functions:
1) To check if the subtree is a perfect Binary Tree
2) Calculate the height of the tree such that all leaves are on the same level
3) Traversing the Tree and check for each node if its subtree is a perfect Binary Tree and if height is positive then insert the nodes, according to the formula for a complete binary tree, in a vector

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

- Space complexity: O(N)
<!-- 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:
    bool pbt(TreeNode* root)
    {
        if(!root->left && !root->right) return true;
        else if(root->left && !root->right) return false;
        else if(!root->left && root->right) return false;

        return pbt(root->left) & pbt(root->right);
    }

    int depth(TreeNode* root)
    {
        if(root == NULL) return 0;
        int l = depth(root->left);
        int r = depth(root->right);

        if(l != r) return INT_MIN;
        return max(l,r) + 1;
    }

    void tra(TreeNode* root, vector<int> &v)
    {
        if(root == NULL) return;

        tra(root->left,v);
        int h = depth(root);
        if(pbt(root) && h>0) v.push_back(pow(2,h) - 1);
        tra(root->right,v);
    }

    int kthLargestPerfectSubtree(TreeNode* root, int k) {
        vector<int> sz;
        tra(root,sz);
        sort(sz.begin(),sz.end(),greater<int>());
        if(k <= sz.size()) return sz[k-1];
        return -1;
    }
};
```
['C++']