[Java] ✅ 5MS ✅ 95% ✅ DFS ✅ COUNT ✅FASTEST ✅ BEST
Approach\n1. Use an int[2001] to store the size count of each subtree. Use count[0] to keep track of how many valid subtrees you got. \n2. Create a recursive fu
# Approach\n1. Use an int[2001] to store the size count of each subtree. Use count[0] to keep track of how many valid subtrees you got. \n2. Create a recursive function that will return the number of left / right subtree node count.\n    - if node is null, the count is 0. \n    - if leaf, the left = 0, right = 0 + current node = 1.\n    - if node is unbalanced, return -1 for this subtree\n2. Apply POST-order traversal and get the count of left and right subtree size for each node.\n    - if both subtrees have diff count then it\'s not balanced. return -1 for current tree\n    - else add their sum + 1 and set inside count[] and return this sum.\n3. If count[0] < k, you don\'t have enough nodes. Else, iterate from 2000 and decrement k while k > 0.\n\n# Complexity\n- Time complexity:$$O(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```java []\nclass Solution {\n    public int kthLargestPerfectSubtree(TreeNode root, int k) {\n        int[] perfectSubtrees = new int[2001];\n        traverseTree(root, perfectSubtrees);\n        if (k > perfectSubtrees[0]) {\n            return -1;\n        }\n        int i = 2001;\n        while (k > 0) {\n            i--;\n            if (perfectSubtrees[i] > 0) {\n                k -= perfectSubtrees[i];\n            }\n        }\n        return i;\n    }\n\n    private int traverseTree(TreeNode node, int[] perfectSubtrees) {\n        if (node == null) {\n            return 0;\n        }\n        int left = traverseTree(node.left, perfectSubtrees);\n        int right = traverseTree(node.right, perfectSubtrees);\n        int subtreeCount = -1;\n        if (left != -1 && right != -1 && left == right) {\n            subtreeCount = 1 + left + right;\n            perfectSubtrees[0]++;\n            perfectSubtrees[subtreeCount]++;\n        }\n        return subtreeCount;\n    }\n}\n```
['Depth-First Search', 'Counting Sort', 'Java']