๐Ÿ“ฆ jtr109 / go-playground

๐Ÿ“„ lib.go ยท 23 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23// https://leetcode.com/problems/trim-a-binary-search-tree/

package trimabinarysearchtree

import "github.com/jtr109/lcutils/treenode"

type TreeNode = treenode.TreeNode

func trimBST(root *TreeNode, low int, high int) *TreeNode {
	if root == nil {
		return nil
	}
	if root.Val < low {
		return trimBST(root.Right, low, high)
	}
	if root.Val > high {
		return trimBST(root.Left, low, high)
	}
	root.Left = trimBST(root.Left, low, high)
	root.Right = trimBST(root.Right, low, high)
	return root
}