Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created August 13, 2018 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/fb926c6db3b844626fa6966d5af65560 to your computer and use it in GitHub Desktop.
Save jianminchen/fb926c6db3b844626fa6966d5af65560 to your computer and use it in GitHub Desktop.
Leetcode 669 - trim a binary search tree
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Leetcode_669_Trim_a_binary_search_tree
{
class Program
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
static void Main(string[] args)
{
}
public TreeNode TrimBST(TreeNode root, int L, int R)
{
if (root == null || L > R)
return null;
var current = root.val;
if (current > R)
return TrimBST(root.left, L, R);
if (current < L)
return TrimBST(root.right, L, R);
// current >= L and current <= R
root.left = TrimBST(root.left, L, R);
root.right = TrimBST(root.right, L, R);
return root;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment