diff --git a/0669_trim-a-binary-search-tree/README.md b/0669_trim-a-binary-search-tree/README.md new file mode 100644 index 0000000..346d94d --- /dev/null +++ b/0669_trim-a-binary-search-tree/README.md @@ -0,0 +1,27 @@ +Given the `root` of a binary search tree and the lowest and highest boundaries as `low` and `high`, trim the tree so that all its elements lies in `[low, high]`. Trimming the tree should **not** change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a **unique answer**. + +Return _the root of the trimmed binary search tree_. Note that the root may change depending on the given bounds. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg) + + Input: root = [1,0,2], low = 1, high = 2 + Output: [1,null,2] + + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg) + + Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3 + Output: [3,2,null,1] + + +**Constraints:** + +* The number of nodes in the tree in the range `[1, 104]`. +* `0 <= Node.val <= 104` +* The value of each node in the tree is **unique**. +* `root` is guaranteed to be a valid binary search tree. +* `0 <= low <= high <= 104` \ No newline at end of file diff --git a/0669_trim-a-binary-search-tree/nodejs/solution.js b/0669_trim-a-binary-search-tree/nodejs/solution.js new file mode 100644 index 0000000..402b9d7 --- /dev/null +++ b/0669_trim-a-binary-search-tree/nodejs/solution.js @@ -0,0 +1,29 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} low + * @param {number} high + * @return {TreeNode} + */ + function trimBST(root, low, high) { + if (!root) { + return null; + } + + if (root.val < low) { + return trimBST(root.right, low, high); + } else if (root.val > high) { + return trimBST(root.left, low, high); + } else { + root.left = trimBST(root.left, low, high); + root.right = trimBST(root.right, low, high); + return root; + } +}; diff --git a/0669_trim-a-binary-search-tree/python3/solution.py b/0669_trim-a-binary-search-tree/python3/solution.py new file mode 100644 index 0000000..0e68c3b --- /dev/null +++ b/0669_trim-a-binary-search-tree/python3/solution.py @@ -0,0 +1,37 @@ +# Time: O(N) +# Space: O(N) due to call stack + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]: + # Recursion approach + + def trim(node): + # Base case + if node is None: + return None + + if node.val < low: + # Cause everything to the left will be + # less than `low` and hence trimmed + return trim(node.right) + + elif node.val > high: + # Cause everything to the right will be + # greater than `high` and hence trimmed + return trim(node.left) + + else: + # `node.val` is between [low, high] so + # just need to trim their left and right + # subtrees + node.left = trim(node.left) + node.right = trim(node.right) + return node + + return trim(root)