/** * 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

相关推荐

点赞 评论 收藏
分享
Aaso:挺好的,早挂早超生
点赞 评论 收藏
分享
牛客网
牛客企业服务