java_solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n    public int kthLargestPerfectSubtree(TreeNode root, int k) {\n        List<Integer>list=new ArrayList<>();\n        int h=height(root);\n        solve(root,list,h);\n        Collections.sort(list, Collections.reverseOrder());\n        if(k>list.size()){\n            return -1;\n        }\n        return list.get(k-1);\n    }\n    int solve(TreeNode root,List<Integer>l,int h){\n        if(root==null){\n            return 0;\n        }\n        int left=solve(root.left,l,h-1);\n        int right=solve(root.right,l,h-1);\n\n        if(left==right ){\n            int act=(int)Math.pow(2,left+1)-1;\n            int count=count(root);\n            if(act==count){\n                int perfectSubtreeSize = (int) Math.pow(2, left + 1) - 1;\n                l.add(perfectSubtreeSize);\n            }\n            \n        }\n        return 1+Math.max(left,right);\n    }\n    int height(TreeNode root){\n        if(root==null){\n            return 0;\n        }\n        return 1+Math.max(height(root.left),height(root.right));\n    }\n    int count(TreeNode root){\n        if(root==null){\n            return 0;\n        }\n        return 1+count(root.left)+count(root.right);\n    }\n}\n```
['Java']