code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

235 Lowest Common Ancestor Of 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 lowestCommonAncestorOfABinarySearchTree = (root, p, q) => {
  if (p.val < root.val && q.val < root.val) return lowestCommonAncestorOfABinarySearchTree(root.left, p, q)
  else if (p.val > root.val && q.val > root.val) return lowestCommonAncestorOfABinarySearchTree(root.right, p, q)
  else return root
}

test('lowestCommonAncestorOfABinarySearchTree', () => {
  const t1 = new TreeNode(6)
  t1.left = new TreeNode(2)
  t1.right = new TreeNode(8)
  t1.left.left = new TreeNode(0)
  t1.left.right = new TreeNode(4)
  t1.left.right.left = new TreeNode(3)
  t1.left.right.right = new TreeNode(5)
  t1.right.left = new TreeNode(7)
  t1.right.right = new TreeNode(9)
  console.log(lowestCommonAncestorOfABinarySearchTree(t1, new TreeNode(2), new TreeNode(8)))
})

Created 2021-01-31T07:21:19+05:30, updated 2021-01-31T07:42:41+05:30 · History · Edit