simple tree traversal
Intuition\n Describe your first thoughts on how to solve this problem. \ncalculate the size of each left and right sub tree \nafter that sorting\n\n# Approach\n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ncalculate the size of each left and right sub tree \nafter that sorting\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)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$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\nvector<int>ans;\n\n    int solve(TreeNode* root){\n        if(!root)return 0;\n        if(!root->left&&!root->right){ans.push_back(1);return 1;}\n\n        int left=solve(root->left);\n        int right=solve(root->right);\n        if(left==-1||right==-1)return -1;\n        if(left==right){ans.push_back(left+right+1);return left+right+1;}\n        return -1;        \n    }\n    int kthLargestPerfectSubtree(TreeNode* root, int k) {\n\n        int t=solve(root);\n        if(k>ans.size())return -1;\n        sort(ans.begin(),ans.end(),greater<int>());\n        return ans[k-1];\n        \n    }\n};\n```
['Tree', 'Binary Tree', 'C++']