Here is the Solution which beats 100% of the Users💯👌😁👏👏
Beats the 100% coders\n\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe problem is about finding the k-th largest perfect 
# Beats the 100% coders\n![Screenshot 2024-10-13 134220.png](https://assets.leetcode.com/users/images/8dcc9664-006a-435e-a3e6-9e057b82e269_1728807182.8651786.png)\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe problem is about finding the k-th largest perfect subtree in a binary tree. A perfect subtree is one where all the nodes have two children except the leaf nodes, and all leaf nodes are at the same depth.The approach is to use Depth-First Search (DFS) to recursively check if a subtree is perfect and keep track of the sizes of perfect subtrees. Finally, we sort the sizes of these subtrees and return the k-th largest.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. DFS Traversal:\n\n- Perform a DFS traversal starting from the root of the tree.\n- For each node, compute the depth of its left and right subtrees.\n- If the left and right subtrees have the same depth, it means the current subtree rooted at this node is perfect.\n- If they aren\'t equal, the subtree is not perfect.\n2. Track Perfect Subtrees:\n\n- When a perfect subtree is found, record its size (calculated as left depth + 1).\n- Store all perfect subtree sizes in the vector a.\n\n3. Sorting and k-th Largest:\n\n- Once the DFS completes, sort the array a in descending order.\n- Check if the size of the array is smaller than k. If yes, return -1 since there aren\'t enough perfect subtrees.\n- Otherwise, return the size of the k-th largest perfect subtree. The size of a perfect subtree of depth d is calculated as (1 << d) - 1, which is the formula for the total number of nodes in a perfect binary tree of depth d.\n\n# Complexity\n- Time complexity:  O(n log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    vector<int> a;\n\n    int dfs(TreeNode* root){\n        if(root == nullptr){\n            return 0;\n        }\n        int left = dfs(root -> left);\n        int right = dfs(root -> right);\n        if(left == -1 || right == -1 || left != right) return -1;\n        a.push_back(left + 1);\n        return left + 1;\n    }\n\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        dfs(root);\n        sort(a.begin(), a.end(), greater<int>());\n        if(a.size() < k) return -1;\n        return (1 << a[k-1]) - 1;\n    }\n};\n```\n# Code of Python\n\n```python\nclass Solution:\n    def __init__(self):\n        self.a = []\n\n    def dfs(self, root):\n        if root is None:\n            return 0\n        left = self.dfs(root.left)\n        right = self.dfs(root.right)\n\n        if left == -1 or right == -1 or left != right:\n            return -1\n\n        self.a.append(left + 1)\n        return left + 1\n\n    def kthLargestPerfectSubtree(self, root, k):\n        self.dfs(root)\n\n        self.a.sort(reverse=True)\n\n        if len(self.a) < k:\n            return -1\n\n        return (1 << self.a[k - 1]) - 1\n```\n\n# Code Of Java\n```Java\n    class Solution {\n    // To store depths of perfect subtrees\n    List<Integer> a = new ArrayList<>();\n\n    public int dfs(TreeNode root) {\n        if (root == null) {\n            return 0;\n        }\n\n        int left = dfs(root.left);\n        int right = dfs(root.right);\n\n        if (left == -1 || right == -1 || left != right) {\n            return -1;\n        }\n        a.add(left + 1);\n        return left + 1;\n    }\n\n    public int kthLargestPerfectSubtree(TreeNode root, int k) {\n        dfs(root);\n        Collections.sort(a, Collections.reverseOrder());\n        if (a.size() < k) {\n            return -1;\n        }\n        return (1 << a.get(k - 1)) - 1;\n    }\n}\n```\n# Please UpVote \u2B06\uFE0F\u2934\uFE0F\uD83D\uDC4D\uD83C\uDD99\n![upvote1.gif](https://assets.leetcode.com/users/images/5aab4d9e-996e-4b40-9220-4e13f7b99992_1728806977.8398988.gif)\n
['Depth-First Search', 'Recursion', 'Sorting', 'Python', 'C++', 'Java']