code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

606 Construct String From Binary Tree

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

function TreeNode(val) {
  this.val = val
  this.left = this.right = null
}

const constructStringFromBinaryTree = root => {
  let result = ''
  const traverse = root => {
    if (root == null) {
      return ''
    }
    let left = root.right ? '()' : ''
    let right = ''
    if (root.left) left = `(${traverse(root.left)})`
    if (root.right) right = `(${traverse(root.right)})`
    return root.val + left + right
  }
  result = traverse(root)
  return result
}

test('constructStringFromBinaryTree', () => {
  const t1 = new TreeNode(1)
  t1.right = new TreeNode(3)
  t1.left = new TreeNode(2)
  t1.left.right = new TreeNode(4)
  expect(constructStringFromBinaryTree(t1)).toEqual('1(2()(4))(3)')
})

Created 2021-01-25T06:01:39+05:18 · Edit