Python3 || 10 lines, dfs  || T/S: 82% / 77%
\npython3 []\nclass Solution:\n    def kthLargestPerfectSubtree(self, root: Optional[TreeNode], k: int) -> int:\n\n        def dfs(node):\n\n            if not 
\n```python3 []\nclass Solution:\n    def kthLargestPerfectSubtree(self, root: Optional[TreeNode], k: int) -> int:\n\n        def dfs(node):\n\n            if not node:return 0\n\n            left, rght = dfs(node.left), dfs(node.right)\n            \n            if left != rght: return -1\n            \n            ans.append(left + rght + 1)\n            return left + rght + 1\n            \n\n        ans = []    \n        dfs(root)\n\n        if len(ans) < k: return -1\n        return nlargest(k,ans)[k-1]\n```\n```cpp []\nclass Solution {\npublic:\n    int dfs(TreeNode* node, vector<int>& ans) {\n        if (!node) return 0;\n        \n        int left = dfs(node->left, ans);\n        int right = dfs(node->right, ans);\n        \n        if (left != right) return -1;\n        \n        ans.push_back(left + right + 1);\n        return left + right + 1;\n    }\n    \n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        vector<int> ans;\n        dfs(root, ans);\n        \n        if (ans.size() < k) return -1;\n        \n        nth_element(ans.begin(), ans.end() - k, ans.end());\n        return ans[ans.size() - k];\n    }\n};\n\n```\n```java []\nclass Solution {\n    private int dfs(TreeNode node, List<Integer> ans) {\n\n        if (node == null) return 0;\n        \n        int left = dfs(node.left, ans);\n        int right = dfs(node.right, ans);\n        \n        if (left != right) return -1;\n        \n        ans.add(left + right + 1);\n        return left + right + 1;}\n    \n    public int kthLargestPerfectSubtree(TreeNode root, int k) {\n        List<Integer> ans = new ArrayList<>();\n        dfs(root, ans);\n        \n        if (ans.size() < k) return -1;\n        \n        Collections.sort(ans, Collections.reverseOrder());\n        return ans.get(k - 1);}\n}\n```\n\n[https://leetcode.com/problems/k-th-largest-perfect-subtree-size-in-binary-tree/submissions/1422446131/](https://leetcode.com/problems/k-th-largest-perfect-subtree-size-in-binary-tree/submissions/1422446131/)\n\nI could be wrong, but I think that time complexity is *O*(*N* log *K*) and space complexity is *O*(*N*), in which *N* ~ `len(nums)` and *K* ~ `k`.
['C++', 'Java', 'Python3']