Problem
class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function treeDepth(node) {
  let leftDepth = treeDepth(node.left);
  let rightDepth = treeDepth(node.right);
  return Math.max(leftDepth, rightDepth) + 1;
}

let root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);

console.log("Depth of the tree:", treeDepth(root));
Solution

The code will throw a TypeError because the treeDepth function is not handling the base case. The base case should be when the node is null, in which case the function should return 0.

class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function treeDepth(node) {
  if (node === null) return 0;
  let leftDepth = treeDepth(node.left);
  let rightDepth = treeDepth(node.right);
  return Math.max(leftDepth, rightDepth) + 1;
}

let root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);

console.log("Depth of the tree:", treeDepth(root));