Created
August 13, 2018 00:11
-
-
Save jianminchen/fb926c6db3b844626fa6966d5af65560 to your computer and use it in GitHub Desktop.
Leetcode 669 - trim a binary search tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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