Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f1c491e

Browse files
author
Swastikyadav
committed
Validate a binary search tree
1 parent 34d93c7 commit f1c491e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Given a node, validate the binary search tree, ensuring that every node's left hand child is less than the parent node's value, and that every node's right hand child is greater than the parent.
3+
*/
4+
5+
function validateBinarySearchTree(node, min=null, max=null) {
6+
if (max !== null && node.data > max) {
7+
return false;
8+
}
9+
10+
if (min !== null && node.data < min) {
11+
return false;
12+
}
13+
14+
if (node.left && !validateBinarySearchTree(node.left, min, node.data)) {
15+
return false;
16+
}
17+
18+
if (node.right && !validateBinarySearchTree(node.right, node.data, max)) {
19+
return false;
20+
}
21+
22+
return true;
23+
}
24+
25+
// This solution use recurtion and I don't like it.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /