O(N) complexity solution with minimal operations
IntuitionDFS with computing height of the perfect binary treeApproach// O(n) time and space complexity solution
// Recursive DFS function returning the height o
# Intuition
DFS with computing height of the perfect binary tree

# Approach
// O(n) time and space complexity solution
    // Recursive DFS function returning the height of the perfect bibary tree
    // and it returns -1 when it is not a perfect binary tree
    // when perfect binary tree, push to a vector and then use std::nth_element which is again O(n), and then ans =  (1<<v[n-k] -1)

# Complexity
- Time complexity:
O(N)

- Space complexity:
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:
    // O(n) time and space complexity solution
    // Recursive DFS function returning the height of the perfect bibary tree
    // and it returns -1 when it is not a perfect binary tree
    // when perfect binary tree, push to a vector and then use std::nth_element which is again O(n), and then ans =  (1<<v[n-k] -1)
    int FindPerfectSubtree(TreeNode* root, std::vector<int>& v) {
        if(root==nullptr) return -1;
        int ret = -1;
        if(root->left == nullptr && root->right==nullptr) ret = 1;
        else {
          int left = FindPerfectSubtree(root->left, v);
          int right = FindPerfectSubtree(root->right, v);
          if(left == right && left != -1) ret = left +1;
        }
        if(ret != -1) v.push_back(ret);
        return ret;
    }
    int kthLargestPerfectSubtree(TreeNode* root, int k) {
        std::vector<int> v;
        FindPerfectSubtree(root, v);
        if(v.size()<k) return -1;
        int n = v.size();
        std::nth_element(v.begin(), v.begin() + n -k, v.end());
        return ((1<<v[n-k]) -1);
    }
};
```
['C++']