DFS Approach 
IntuitionIf left tree and right tree are both balanced then curr tree is also balancedApproach
if left tree or right tree is not balanced, curr tree cannot be b
# Intuition
If left tree and right tree are both balanced then curr tree is also balanced
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
- if left tree or right tree is not balanced, curr tree cannot be balanced, return -1 in this case.
- If both height are returned positive means both tree are balanced, just check if both have equal numb. of nodes,if yes then curr tree will also be balanced, store number of nodes of this subtree in the arr.
- sort arr in desc and return kth node.
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:O(N) + N*O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

class Solution {
    public int kthLargestPerfectSubtree(TreeNode root, int k) {
        ArrayList<Integer> arr=new ArrayList<>();
        dfs(arr,root);
        Collections.sort(arr,Collections.reverseOrder());
        // for(int n:arr) System.out.println(n+" ");
        if(arr.size()<k) return -1;
        else return arr.get(k-1);
        
    }
    public int dfs(ArrayList<Integer> arr,TreeNode root){
        if(root==null) return 0;

        int lh=dfs(arr,root.left);
        int rh=dfs(arr,root.right);
        if(lh<0 || rh<0) return -1;
        if(lh==rh){ 
        arr.add(lh+rh+1);
        return lh+rh+1 ;
        }
        return -1;
        
    }
}
```
['Tree', 'Depth-First Search', 'Java']