C++ Solution Beats 98% in runtime || Tree Traversal
Intuition\n Describe your first thoughts on how to solve this problem. \nTraverses the tree using post order traversal and calculate size at each node and check
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTraverses the tree using post order traversal and calculate size at each node and check its left and right subtree.\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)$$ -->\nO(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(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    int solve (TreeNode* root,vector<int> &v) {\n        if (root==NULL) {\n            return 0;\n        }\n        int left=solve(root->left,v);\n        int right=solve(root->right,v);\n        if (left==right && left!=-1) {\n            int size=left+right+1;\n            v.push_back(size);\n            return (size);\n        } else {\n            return -1;\n        }\n    }\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n        vector<int> v;\n        solve(root,v);\n        sort(v.begin(),v.end());\n        if (v.size()>=k) {\n            return v[v.size()-k];\n        } else {\n            return -1;\n        }\n    }\n};\n```
['C++']