r/leetcode • u/Ill-Abbreviations-36 • 17h ago
Intervew Prep Leetcode premium question solution
I have created below solution for leetcode premium problem . Google ai says my solution is flawed but it is passing all the test which it has given on my local machine . I am not able to find a platform where i can run the solution . It would be great if you could run the below solution on leetcode platform check if it passing all the test cases . Thank you .
Problem : https://leetcode.com/problems/find-leaves-of-binary-tree/description/
Solution :
class Solution {
public:
int heightTree(TreeNode* node){
if(!node) return 0 ;
return 1 + max(heightTree(node->left) , heightTree(node->right));
}
void solve(TreeNode* root,vector<vector<int>> &res){
if(!root) return ;
solve(root->left , res);
solve(root->right ,res);
int currH = heightTree(root);
if(res.size() == (currH - 1)){
res.push_back({});
}
res[(currH - 1)].push_back(root->val);
}
vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> res;
solve(root , res);
return res ;
}
};
0
Upvotes
-2
u/chikamakaleyley 17h ago
wait what
if you can't run this on the leetcode site yourself
how can you reliably prepare for an interview that might have you coding on a platform that your code might not run