|
| 1 | +/* |
| 2 | + Write a function that takes in a potentially invalid Binary Search Tree (BST) and returns a boolean representing |
| 3 | + whether the BST is valid. |
| 4 | +
|
| 5 | + Explanation: |
| 6 | +
|
| 7 | + This code defines a Binary Search Tree (BST) struct with an integer value and left and right nodes that can point to other |
| 8 | + BST nodes. The struct also has a method called ValidateBst() that returns a boolean indicating whether the tree is a valid |
| 9 | + BST or not. |
| 10 | +
|
| 11 | + The BST struct has another method called validateBST() that is used by ValidateBst() to check whether the tree is a valid |
| 12 | + BST or not. The validateBST() method takes in two arguments, min and max, which represent the minimum and maximum values |
| 13 | + that the current node's value can take in order to be a valid BST. |
| 14 | +
|
| 15 | + The validateBST() method first checks whether the current node's value is within the valid range determined by the min and |
| 16 | + max arguments. If not, the method returns false, indicating that the tree is not a valid BST. |
| 17 | +
|
| 18 | + If the current node's value is within the valid range, the method then recursively calls itself on the left and right |
| 19 | + child nodes to check whether their values are within their valid ranges. The valid range for the left child node is defined by the minimum value and the parent node's value, while the valid range for the right child node is defined by the parent node's value and the maximum value. |
| 20 | +
|
| 21 | + If all of the nodes in the tree satisfy the BST property, the method returns true, indicating that the tree is a valid BST. |
| 22 | +
|
| 23 | + O(n) time | O(d) space - where n is the number of nodes in the BST and d is the depth (height) of the BST |
| 24 | +*/ |
| 25 | +class TreeNode { |
| 26 | + constructor(value) { |
| 27 | + this.value = value; |
| 28 | + this.left = null; |
| 29 | + this.right = null; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// Function to check if the BST is valid |
| 34 | +function isValidBST(root) { |
| 35 | + return isValidBSTHelper(root, -Infinity, Infinity); |
| 36 | +} |
| 37 | + |
| 38 | +// Recursive helper function to check if the BST is valid |
| 39 | +function isValidBSTHelper(node, minVal, maxVal) { |
| 40 | + // Base case: if the current node's value is outside the allowed range, then the tree is invalid |
| 41 | + if (!node || node.value <= minVal || node.value >= maxVal) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + // Recursively check the left subtree, making sure all values are less than the current node's value |
| 46 | + if (!isValidBSTHelper(node.left, minVal, node.value)) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + // Recursively check the right subtree, making sure all values are greater than or equal to the current node's value |
| 51 | + if (!isValidBSTHelper(node.right, node.value, maxVal)) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + // If we reach this point, then the tree is valid |
| 56 | + return true; |
| 57 | +} |
| 58 | + |
| 59 | +// Example usage: |
| 60 | +const root = new TreeNode(10); |
| 61 | +root.left = new TreeNode(5); |
| 62 | +root.right = new TreeNode(15); |
| 63 | +root.left.left = new TreeNode(2); |
| 64 | +root.left.right = new TreeNode(7); |
| 65 | + |
| 66 | +if (isValidBST(root)) { |
| 67 | + console.log("The BST is valid."); |
| 68 | +} else { |
| 69 | + console.log("The BST is not valid."); |
| 70 | +} |
0 commit comments