===== ISTEM =====
Solve the following programming task.

TASK: Memoize Function Results

Write a pure 'memoize(fn)' wrapper that returns the cached result for the same arguments instead of recomputing it.

STARTER CODE (javascript):
function memoize(fn) {
  // Write your code here
}

module.exports = { memoize };


RULES:
- Keep the function name and signature EXACTLY as given.
- Return working code only. No explanations.
- Put the code in a single ``` block.
- Try to solve it first. If you genuinely cannot, write only this single
  line instead of producing faulty code: CANNOT_SOLVE

===== HAM YANIT =====
```javascript
function memoize(fn) {
  const root = { children: new Map(), cached: false, value: undefined };

  return function (...args) {
    let node = root;

    for (const arg of args) {
      if (!node.children.has(arg)) {
        node.children.set(arg, {
          children: new Map(),
          cached: false,
          value: undefined
        });
      }
      node = node.children.get(arg);
    }

    if (!node.cached) {
      node.value = fn.apply(this, args);
      node.cached = true;
    }

    return node.value;
  };
}

module.exports = { memoize };
```