|
| 1 | +# Validate Binary Search Tree |
| 2 | + |
| 3 | +Page on leetcode: https://leetcode.com/problems/validate-binary-search-tree/ |
| 4 | + |
| 5 | +## Problem Statement |
| 6 | + |
| 7 | +Given the root of a binary tree, determine if it is a valid binary search tree (BST). |
| 8 | + |
| 9 | +A valid BST is defined as follows: |
| 10 | + |
| 11 | +- The left subtree of a node contains only nodes with keys less than the node's key. |
| 12 | +- The right subtree of a node contains only nodes with keys greater than the node's key. |
| 13 | +- Both the left and right subtrees must also be binary search trees. |
| 14 | + |
| 15 | +### Constraints |
| 16 | + |
| 17 | +- The number of nodes in the tree is in the range [1, 104]. |
| 18 | +- -231 <= Node.val <= 231 - 1 |
| 19 | + |
| 20 | +### Example |
| 21 | + |
| 22 | +``` |
| 23 | +Input: root = [2,1,3] |
| 24 | +Output: true |
| 25 | +``` |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +- Probably use DFS to traverse the tree |
| 30 | +- Might be able to exit early if we find any node that doesn't satisfy BST |
| 31 | +- if null return |
| 32 | +- if left and right val are null return true |
| 33 | +- if left < val or left == null and right > val or right == null return true |
| 34 | +- else return false |
| 35 | + |
| 36 | +### Pseudocode |
| 37 | + |
| 38 | +1. base case if root is null return true |
| 39 | +2. l = dfs(left) and r = dfs(right) |
| 40 | +3. if left and right are null return true |
| 41 | +4. if left.val < val or left == null and right.val > val or right == null and l and r are true return true |
| 42 | +5. else return false |
| 43 | + |
| 44 | +### Initial Attempt |
| 45 | + |
| 46 | +```javascript |
| 47 | +const isValidBST = function (root) { |
| 48 | + if (!root) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + const l = isValidBST(root.left); |
| 52 | + const r = isValidBST(root.right); |
| 53 | + const lVal = root.left ? root.left.val < root.val : true; |
| 54 | + const rVal = root.right ? root.right.val > root.val : true; |
| 55 | + |
| 56 | + if (lVal && rVal && l && r) { |
| 57 | + return true; |
| 58 | + } else { |
| 59 | + return false; |
| 60 | + } |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +### Optimized Solution |
| 65 | + |
| 66 | +The time and space complexity for both solutions below is O(n). Using in order traversal is likely a better approach. You can see an explanation of this solution here: https://www.youtube.com/watch?v=s6ATEkipzow |
| 67 | + |
| 68 | +```javascript |
| 69 | +// Recursive DFS |
| 70 | +const isValidBST = function (root) { |
| 71 | + function valid(root, left, right) { |
| 72 | + // Base case, null node is true |
| 73 | + if (!root) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + // Recursive calls on left and right nodes passing update "boundaries" |
| 77 | + const lNode = valid(root.left, left, root.val); |
| 78 | + const rNode = valid(root.right, root.val, right); |
| 79 | + |
| 80 | + // Check that current node satisfies boundaries and child nodes are BST |
| 81 | + if (left < root.val && right > root.val && lNode && rNode) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + return valid(root, -Infinity, Infinity); |
| 89 | +}; |
| 90 | + |
| 91 | +// In order traversal |
| 92 | +const isValidBST = function (root) { |
| 93 | + // Create the empty stack and set previous to null |
| 94 | + const stack = []; |
| 95 | + let prev = null; |
| 96 | + |
| 97 | + while (root !== null || stack.length > 0) { |
| 98 | + // Traverse down the left nodes as far as possible and add to stack |
| 99 | + while (root !== null) { |
| 100 | + stack.push(root); |
| 101 | + root = root.left; |
| 102 | + } |
| 103 | + |
| 104 | + root = stack.pop(); |
| 105 | + // Checking if the previous value is larger than current, if it is then this is not a BST |
| 106 | + if (prev !== null && prev.val >= root.val) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + // Update previous to the current root and then move root to the right |
| 110 | + prev = root; |
| 111 | + root = root.right; |
| 112 | + } |
| 113 | + // If we make it all the way here and didn't return false then it is a BST |
| 114 | + return true; |
| 115 | +}; |
| 116 | +``` |
0 commit comments