DFS Traversal + Image Explanation || Beginner Friendly || C++
\n\n\n\n\nint dfs_khLargestPerfectSubtree(TreeNode* root, vector<int>& values) {\n    if (!root)\n        return 0;\n\n    int right = dfs_khLargestPerfectSubtr
![image](https://assets.leetcode.com/users/images/13d96cf3-859c-4851-85ce-1a46cfa333af_1728798479.5648658.jpeg)\n\n\n\n```\nint dfs_khLargestPerfectSubtree(TreeNode* root, vector<int>& values) {\n    if (!root)\n        return 0;\n\n    int right = dfs_khLargestPerfectSubtree(root->right, values);\n    int left = dfs_khLargestPerfectSubtree(root->left, values);\n\n    int count_nodes = -1;\n    if (right == left) {\n        count_nodes = (left + right) + 1;\n        values.push_back(count_nodes);\n    }\n\n    return count_nodes;\n}\n\nint kthLargestPerfectSubtree(TreeNode* root, int k) {\n    vector<int> values;\n\n    int val = dfs_khLargestPerfectSubtree(root, values);\n    sort(values.begin(), values.end(), greater<int>());\n    return k > values.size() ? -1 : values[k - 1];\n}\n```
['Depth-First Search', 'C']