/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return TreeNode类 */ int process(int x, int base){ int cn = 0; while(x%base == 0){ x /= base; cn++; } return cn; } typedef pair<int, int>PII; int t[100010], f[100010]; PII dfs(TreeNode* root){ if(!root)return {0, 0}; auto left = dfs(root->left); auto right = dfs(root->right); int val = root->val; int five = process(val, 5); int two = process(val, 2); // if(left.first == -1 || right.first == -1){ // root->val = 0; // return // } root->val = min(left.first+two+right.first, left.second+five+right.second); return {left.first+two+right.first, left.second+five+right.second}; } TreeNode* valueOfTree(TreeNode* root) { // write code here dfs(root); return root; } };
1 1

相关推荐

兑生:一开始只是想找个实习,找到实习了想进大厂,进大厂了想转正,转正了想offer打牌,打牌了想要大包,大包了想要wlb……
点赞 评论 收藏
分享
01-05 09:14
同济大学 Java
不要盒我呀:我要是9✌🏻我就选保研,保研了大四再找实习,实习之后,如果觉得自己不适合互联网工作模式,还能有其他选择,如果实习后决定了走互联网,也能提升学历提高竞争力
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务