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

669 Trim A Binary Search Tree

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

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

const trimABinarySearchTree = (root, low, high) => {
  if (root == null) {
    return null
  }
  if (root.val < low) {
    return trimABinarySearchTree(root.right, low, high)
  }
  if (root.val > high) {
    return trimABinarySearchTree(root.left, low, high)
  }
  root.left = trimABinarySearchTree(root.left, low, high)
  root.right = trimABinarySearchTree(root.right, low, high)
  return root
}

test('trimABinarySearchTree', () => {
  const t1 = new TreeNode(3)
  t1.right = new TreeNode(4)
  t1.left = new TreeNode(0)
  t1.left.right = new TreeNode(2)
  t1.left.right.left = new TreeNode(1)

  const t2 = new TreeNode(3)
  t2.left = new TreeNode(2)
  t2.left.left = new TreeNode(1)
  expect(trimABinarySearchTree(t1, 1, 3)).toEqual(t2)
})

Created 2020-12-07T18:08:50+05:18 · Edit