Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created September 3, 2017 02:11
Show Gist options
  • Save cixuuz/580bd4834118cd291913828e96d71180 to your computer and use it in GitHub Desktop.
Save cixuuz/580bd4834118cd291913828e96d71180 to your computer and use it in GitHub Desktop.
[669. Trim a Binary Search Tree] #leetcode
class Solution {
// O(n) O(n)
public TreeNode trimBST(TreeNode root, int L, int R) {
if (root == null) return null;
if (root.val > R) return trimBST(root.left, L, R);
if (root.val < L) return trimBST(root.right, L, R);
root.left = trimBST(root.left, L, root.val);
root.right = trimBST(root.right, root.val, R);
return root;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment