Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 489 Bytes

trim-a-binary-search-tree.md

File metadata and controls

32 lines (26 loc) · 489 Bytes

Code

/**
 * Definition for 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
}