# Node Depths

<figure><img src="https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-fecb1f2ba4d201ac1e78bdecc05467007668e58c%2FScreenshot%202023-01-20%20at%2018.30.46.png?alt=media" alt=""><figcaption></figcaption></figure>

```javascript
class DepthSum {
  constructor() {
    this.sum = 0;
  }
}

function nodeDepths(root) {
  const memo = new DepthSum();
  findNodeDepthSum(root, memo)
  return memo.sum;
}

function findNodeDepthSum(node, memo, depth = 0) {
  if (node === null) {
    // return depth;
    return;
  }

  memo.sum += depth;

  const left = findNodeDepthSum(node.left, memo, depth + 1);
  const right = findNodeDepthSum(node.right, memo, depth + 1);

  // return left + right;
  return;
}

// This is the class of the input binary tree.
class BinaryTree {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

// Do not edit the line below.
exports.nodeDepths = nodeDepths;
```
