JavaScript solution | DFS
Complexity
Time complexity: O(n)
Space complexity: O(n)
Code
# Complexity
- Time complexity: O(n)
- Space complexity: O(n)

# Code
```javascript []
var kthLargestPerfectSubtree = function(root, k) {
    let arr = [];

    function dfs(node){
        if(!node) return 0;
        let left = dfs(node.left);
        let right = dfs(node.right); 
        if(left >= 0 && left == right){
            arr.push(1 + left + right);
            return 1 + left + right;
        } else return -1;
    }
    
    dfs(root);
    
    if(arr.length < k) return -1;

    arr.sort((a,b) => b-a);
    return arr[k-1];
};
```
['Tree', 'Depth-First Search', 'Sorting', 'Binary Tree', 'JavaScript']