Beats 100% faster Sol || Simple Tree Traversal and Easy Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->O(n)\n\n# Code\n```cpp []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    pair<int,bool> formingArr(TreeNode* root,vector<int>&ans){\n        if(root==NULL){\n            return {0,true};\n        }\n        pair<int,bool> leftHeight=formingArr(root->left,ans);\n        pair<int,bool> rightHeight=formingArr(root->right,ans);\n\n        pair<int,bool>check;\n        if(leftHeight.second && rightHeight.second && (leftHeight.first==rightHeight.first)){\n            check.first=leftHeight.first+rightHeight.first+1;\n            ans.push_back(check.first);\n            check.second=true;\n        }\n        else{\n         //   check.first = max(leftHeight.first, rightHeight.first) + 1; \n            check.second=false;\n        }\n        return check;\n    }\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        vector<int>ans;\n        formingArr(root,ans);\n        sort(ans.begin(),ans.end(),greater<int>());\n        if (k > ans.size()) {\n            return -1; // Handle the case when k is out of bounds\n        }\n        return ans[k-1];\n    }\n};\n```
['C++']