Category: GCD-LCM

Minimize sum of node values by filling given empty Tree such that each node is GCD of its children

#include using namespace std;  class Node {public:    int val;    Node *left, *right;    Node(int val)    {        this->val = val;        left = NULL;        right = NULL;    }};  class Tree {public:    unordered_map depth;              int findDepth(Node* cur)    {        int mx = 0;        if (cur->left) {            mx = findDepth(cur->left);        }        if (cur->right) {            mx = max(mx, findDepth(cur->right));        }                          return depth[cur] = mx + 1;    }              int dfs(Node* cur, bool flag, int parValue)    {        if (parValue != -1) {            if (flag)                cur->val = parValue;            else                cur->val = parValue * 2;        }        int […]

Continue Reading