O(n) time complexity dfs Java
Intuition\n Describe your first thoughts on how to solve this problem. \nDo dfs, visit each node, store count of nodes in perfect binary tree in priority queue 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDo dfs, visit each node, store count of nodes in perfect binary tree in priority queue and return kth largest nodes\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nDo dfs, fetcb\n\n# Complexity\n- Time complexity:\n- $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- $$O(n)$$\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    PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){\n        public int compare(Integer a, Integer b)\n        {\n            return b-a;\n        }\n    });\n    public int kthLargestPerfectSubtree(TreeNode root, int k) {\n        dfs(root);\n        int c = 0;\n        int ans = 0;\n        while(c<k && !pq.isEmpty())\n        {\n            ans = pq.poll();\n            c++;\n        }\n        if(pq.isEmpty() && c<k)\n            return -1;\n        return ans;\n    }\n    int dfs(TreeNode root)\n    {\n        if(root==null)\n            return 0;\n        int left = dfs(root.left);\n        int right = dfs(root.right);\n        if(left==-1||right==-1)\n            return -1;\n        if(left==right)\n        {\n            pq.offer(left+right+1);\n            return left + right + 1;\n        }\n        else\n        {\n            return -1;\n        }\n        \n    }\n}\n```
['Java']