Medium made Easy | Beats 100% | Single DFS | C++
Intuition\nA prefect Binary tree of height h has (2^h - 1) nodes.\nh = 1, for single node.\n Describe your first thoughts on how to solve this problem. \n\n# Ap
# Intuition\nA prefect Binary tree of height h has (2^h - 1) nodes.\nh = 1, for single node.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nModify normal dfs to return both count of nodes in a tree/subtree & its height.\n<!-- Describe your approach to solving the problem. -->\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```cpp []\nclass Solution {\npublic:\n\n    vector<int> v;\n\n    pair<int,int> dfs(TreeNode *root){  // return tot_nodes, height\n        if(!root){return {0,0};}\n        pair<int,int> p1 = {0,0}, p2 = p1;\n\n        if(root->left){p1 = dfs(root->left);}\n        if(root->right){p2 = dfs(root->right);}\n        \n        int tot_nodes = p1.first + p2.first + 1;\n        int height = max(p1.second, p2.second) + 1;\n\n        int tmp = pow(2, height) - 1;\n        // checking for Perfect Binary tree\n        if(tot_nodes==tmp){v.push_back(tot_nodes);}  \n\n        return {tot_nodes, height};\n    }\n\n    int kthLargestPerfectSubtree(TreeNode* root, int k){\n        dfs(root);\n        sort(v.rbegin(), v.rend());\n        if(v.size()<k){return -1;}\n        return v[k-1];\n    }\n};\n```
['Greedy', 'Tree', 'Depth-First Search', 'Recursion', 'Sorting', 'Simulation', 'Ordered Set', 'C++']