Skip to content

Commit 4fe6804

Browse files
committed
✅ [98] recursion
1 parent a14b988 commit 4fe6804

File tree

2 files changed

+25
-45
lines changed

2 files changed

+25
-45
lines changed

98/my_solution.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TreeNode {
1111
* @return {boolean}
1212
*/
1313
var isValidBST = function (root) {
14-
let leftAncestors = [], rightAncestors = [];
14+
let leftAncestors = [root.val], rightAncestors = [root.val];
1515

1616
const exploreLeft = (node, list) => {
1717
console.log(`exploreLeft`)
@@ -34,25 +34,32 @@ var isValidBST = function (root) {
3434
return true || exploreLeft(node, list);
3535
}
3636

37-
let queue = [root];
37+
const explore = (queue, list) => {
38+
while (queue.length > 0) {
39+
let curr = queue.shift(), left = curr.left, right = curr.right;
3840

39-
while (queue.length > 0) {
40-
let curr = queue.shift(), left = curr.left, right = curr.right;
41+
if ((left?.val ?? -Infinity) >= curr.val || (right?.val ?? Infinity) <= curr.val) return false;
4142

42-
if ((left?.val ?? -Infinity) >= curr.val || (right?.val ?? Infinity) <= curr.val) return false;
43-
44-
if (null !== left) {
45-
queue.push(left);
46-
if (!exploreLeft(left, leftAncestors)) return false;
47-
}
43+
if (null !== left) {
44+
queue.push(left);
45+
if (!exploreLeft(left, list)) return false;
46+
}
4847

4948

50-
if (null !== right) {
51-
queue.push(right);
52-
if (!exploreRight(right, rightAncestors)) return false;
49+
if (null !== right) {
50+
queue.push(right);
51+
if (!exploreRight(right, list)) return false;
52+
}
5353
}
5454
}
5555

56+
if (root.left !== null) {
57+
explore([root.left], leftAncestors);
58+
}
59+
if (root.right !== null) {
60+
explore([root.right], rightAncestors);
61+
}
62+
5663
return true;
5764
};
5865

98/solution.js

+5-32
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,11 @@ class TreeNode {
1111
* @return {boolean}
1212
*/
1313
var isValidBST = function (root) {
14-
let leftAncestors = [], rightAncestors = [];
15-
16-
const exploreLeft = (node) => {
17-
if (node.val !== Math.min(leftAncestors.concat([node.val]))) return false;
18-
leftAncestors.push(node.val);
19-
return true;
20-
}
21-
22-
const exploreRight = (node) => {
23-
if (node.val !== Math.max(rightAncestors.concat([node.val]))) return false;
24-
rightAncestors.push(node.val);
25-
return true;
26-
}
27-
28-
let queue = [root];
29-
30-
while (queue.length > 0) {
31-
let curr = queue.shift(), left = curr.left, right = curr.right;
32-
33-
if ((left?.val ?? -Infinity) >= curr.val || (right?.val ?? Infinity) <= curr.val) return false;
34-
35-
if (null !== left) {
36-
queue.push(left);
37-
if (!exploreLeft(left)) return false;
38-
}
39-
40-
41-
if (null !== right) {
42-
queue.push(right);
43-
if (!exploreRight(right)) return false;
44-
}
14+
const isTreeValid = (node, min, max) => {
15+
if (null === node) return true;
16+
if (!(min < node.val && node.val < max)) return false;
17+
return isTreeValid(node.left, min, node.val) && isTreeValid(node.right, node.val, max);
4518
}
4619

47-
return true;
20+
return isTreeValid(root.left, -Infinity, root.val) && isTreeValid(root.right, root.val, Infinity);
4821
};

0 commit comments

Comments
 (0)