Question
PS: This is an interview question in real. After the interview, I optimized my answer of the interview and share with you guys here.
Give you a number, and a Binary Search Tree, please find the number or get its nearest node value out of the tree. Can check the UT as examples.
UT
No need to Copy and Paste here. There is a full version below for your Copy and Paste. Keep scroll down.
QUnit.module('Search BSTree', function() {
const run = (tree, search) => new Solution(tree, search).solve()
QUnit.test('binary search tree', function(assert) {
const root = createBTree([10,5,15,1,6,13,17])
// 10
// / \
// 5 15
// / \ / \
// 1 6 13 17
assert.equal(run(root, 9), 10)
assert.equal(run(root, 4), 5)
assert.equal(run(root, 6), 6)
assert.equal(run(root, 7), 6)
assert.equal(run(root, 20), 17)
assert.equal(run(root, 14), 15)
assert.equal(run(root, 17), 17)
assert.equal(run(root, 12), 13)
});
});
Answer
Thinking: We have to walk through the BSTree, and find the value. If fount, then return the value directly, of course, it’s the nearest already. Otherwise, check the child’s tree nearest value vs current node’s value who is nearer to the supplied number.
Main logic
Just for read. Scroll down to see full code.
class Solution {
constructor(tree, num) {
this.tree = tree
this.num = num
}
bs(root, num) {
let nearest = null
if (num === root.val) return root.val
if (num < root.val) {
if (root.left) {
nearest = this.bs(root.left, num)
}
} else {
if (root.right) {
nearest = this.bs(root.right, num)
}
}
if (Math.max(nearest - num, num - nearest) < Math.max(root.val - num, num - root.val)) {
return nearest
} else {
return root.val
}
}
solve() {
return this.bs(this.tree, this.num)
}
}
Full code with UT
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Binary search tree</title>
<link href="https://code.jquery.com/qunit/qunit-git.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/qunit/qunit-git.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script>
class Solution {
constructor(tree, num) {
this.tree = tree
this.num = num
}
bs(root, num) {
let nearest = null
if (num === root.val) return root.val
if (num < root.val) {
if (root.left) {
nearest = this.bs(root.left, num)
}
} else {
if (root.right) {
nearest = this.bs(root.right, num)
}
}
if (Math.max(nearest - num, num - nearest) < Math.max(root.val - num, num - root.val)) {
return nearest
} else {
return root.val
}
}
solve() {
return this.bs(this.tree, this.num)
}
}
QUnit.module('Search BSTree', function() {
const run = (tree, search) => new Solution(tree, search).solve()
QUnit.test('binary search tree', function(assert) {
const root = createBTree([10,5,15,1,6,13,17])
// 10
// / \
// 5 15
// / \ / \
// 1 6 13 17
assert.equal(run(root, 9), 10)
assert.equal(run(root, 4), 5)
assert.equal(run(root, 6), 6)
assert.equal(run(root, 7), 6)
assert.equal(run(root, 20), 17)
assert.equal(run(root, 14), 15)
assert.equal(run(root, 17), 17)
assert.equal(run(root, 12), 13)
});
});
// Tools
class Node {
constructor(val, left = null, right = null) {
this.val = val
this.left = left
this.right = right
}
}
function createBTree(arr) {
const nodes = arr.map(val => val === null ? null : new Node(val))
for (let i = 0; i < nodes.length; i++) {
if (nodes[i] === null) continue
nodes[i].left = nodes[i*2+1] || null
nodes[i].right = nodes[i*2+2] || null
}
return nodes[0]
}
</script>
</body>
</html>
PS: Copy and paste this code in your own HTML file, don’t forget to serve it as a server. You might know that I mean. (CORS won’t allow you to load QUnit.
Result
Finally
You are welcome to tell me if any bugs or thoughts or discussions.
Please free to leave message to me.
_
Fighting!