Skip to content

Commit 87a2ee2

Browse files
committed
add iterative solution for validate bst question
1 parent 35a39d9 commit 87a2ee2

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

binary-trees/validate-bst.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function validateBstHelper(tree, minValue, maxValue) {
2121
//
2222
// SOLUTION 2
2323
// This solution is iterative, same time complexity using DFS approach
24-
// Time O(n)
24+
// Time O(n) - where n is the number of nodes
2525
// Space O(d) - where d is the depth of the tree
2626
//
2727
function validateBst(root) {
@@ -33,7 +33,6 @@ function validateBst(root) {
3333
while (stack.length > 0) {
3434
const currentNode = stack.pop();
3535
if (
36-
currentNode === null ||
3736
currentNode.value < currentNode.min ||
3837
currentNode.value >= currentNode.max
3938
)
@@ -43,7 +42,7 @@ function validateBst(root) {
4342
currentNode.left.max = currentNode.value;
4443
stack.push(currentNode.left);
4544
}
46-
if (currentNode.right !== null) {
45+
if (currentNode.right) {
4746
stack.push(currentNode.right);
4847
currentNode.right.min = currentNode.value;
4948
currentNode.right.max = currentNode.max;

0 commit comments

Comments
 (0)