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

653 Two Sum Iv Input Is Bst

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

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

const twoSumIvInputIsBst = (root, target) => {
  let found = false
  let map = {}
  const traverse = root => {
    if (!root || found) {
      return
    }
    const complement = target - root.val
    if (map[complement]) {
      found = true
      return
    } else {
      map[root.val] = root.val
    }
    traverse(root.left)
    traverse(root.right)
  }
  traverse(root)
  return found
}

test('twoSumIvInputIsBst', () => {
  const t1 = new TreeNode(5)
  t1.right = new TreeNode(6)
  t1.right.right = new TreeNode(7)
  t1.left = new TreeNode(3)
  t1.left.right = new TreeNode(4)
  t1.left.left = new TreeNode(2)
  expect(twoSumIvInputIsBst(t1, 9)).toEqual(true)
})

Created 2020-12-12T01:31:23+05:18 · Edit