const printTree = (root) => {
  function traverseInOrderHelper(padding, pointer, node, hasRightSibling) {
    if (!node) return

    sb += '\n'
    sb += padding
    sb += pointer
    sb += node.val

    let paddingBuilder = padding
    if (hasRightSibling) {
      paddingBuilder += '│  '
    } else {
      paddingBuilder += '   '
    }

    let paddingForBoth = paddingBuilder
    let pointerForRight = '└──'
    let pointerForLeft = node.right !== undefined ? '├──' : '└──'

    traverseInOrderHelper(
      paddingForBoth,
      pointerForLeft,
      node.left,
      root.right !== undefined
    )
    traverseInOrderHelper(paddingForBoth, pointerForRight, node.right, false)
  }

  let sb = ''

  sb += root.val

  let pointerRight = '└──'
  let pointerLeft = root.right !== undefined ? '├──' : '└──'

  traverseInOrderHelper('', pointerLeft, root.left, root.right !== undefined)
  traverseInOrderHelper('', pointerRight, root.right, false)

  return sb
}

module.exports = printTree
