From 73268dbc1249eea1969a786341f25254e0691349 Mon Sep 17 00:00:00 2001 From: Yaismel Miranda <yaismel.miranda@gmail.com> Date: Tue, 9 Feb 2021 23:20:48 -0500 Subject: [PATCH] leetcode: 669. Trim a Binary Search Tree --- .../0669-trim-a-binary-search-tree/README.md | 18 ++++++ .../trim_bst.go | 23 ++++++++ .../trim_bst_test.go | 56 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 leetcode/0669-trim-a-binary-search-tree/README.md create mode 100644 leetcode/0669-trim-a-binary-search-tree/trim_bst.go create mode 100644 leetcode/0669-trim-a-binary-search-tree/trim_bst_test.go diff --git a/leetcode/0669-trim-a-binary-search-tree/README.md b/leetcode/0669-trim-a-binary-search-tree/README.md new file mode 100644 index 0000000..3a35874 --- /dev/null +++ b/leetcode/0669-trim-a-binary-search-tree/README.md @@ -0,0 +1,18 @@ +# 669. Trim a Binary Search Tree + +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:** +``` +Input: root = [1,0,2], low = 1, high = 2 +Output: [1,null,2] +``` + +**Constraints:** +- The number of nodes in the tree in the range `[1, 10^4]`. +- `0 <= Node.val <= 10^4` +- The value of each node in the tree is **unique**. +- `root` is guaranteed to be a valid binary search tree. +- `0 <= low <= high <= 10^4` diff --git a/leetcode/0669-trim-a-binary-search-tree/trim_bst.go b/leetcode/0669-trim-a-binary-search-tree/trim_bst.go new file mode 100644 index 0000000..385b832 --- /dev/null +++ b/leetcode/0669-trim-a-binary-search-tree/trim_bst.go @@ -0,0 +1,23 @@ +package leetcode + +// TreeNode defines a binary tree node. +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func trimBST(root *TreeNode, low int, high int) *TreeNode { + if root == nil { + return root + } + if root.Val > high { + return trimBST(root.Left, low, high) + } + if root.Val < low { + return trimBST(root.Right, low, high) + } + root.Left = trimBST(root.Left, low, high) + root.Right = trimBST(root.Right, low, high) + return root +} diff --git a/leetcode/0669-trim-a-binary-search-tree/trim_bst_test.go b/leetcode/0669-trim-a-binary-search-tree/trim_bst_test.go new file mode 100644 index 0000000..8c3cd5f --- /dev/null +++ b/leetcode/0669-trim-a-binary-search-tree/trim_bst_test.go @@ -0,0 +1,56 @@ +package leetcode + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTrimBST(t *testing.T) { + assert := assert.New(t) + for _, test := range tests { + assert.Equal(test.expected, trimBST(test.root, test.low, test.high)) + } +} + +var tests = []struct { + root *TreeNode + low, high int + expected *TreeNode +}{ + { + root: &TreeNode{ + Val: 1, + Left: &TreeNode{Val: 0}, + Right: &TreeNode{Val: 2}, + }, + low: 1, + high: 2, + expected: &TreeNode{ + Val: 1, + Right: &TreeNode{Val: 2}, + }, + }, + { + root: &TreeNode{ + Val: 3, + Left: &TreeNode{ + Val: 0, + Right: &TreeNode{ + Val: 2, + Left: &TreeNode{Val: 1}, + }, + }, + Right: &TreeNode{Val: 4}, + }, + low: 1, + high: 3, + expected: &TreeNode{ + Val: 3, + Left: &TreeNode{ + Val: 2, + Left: &TreeNode{Val: 1}, + }, + }, + }, +}