Created
September 3, 2017 02:11
-
-
Save cixuuz/580bd4834118cd291913828e96d71180 to your computer and use it in GitHub Desktop.
[669. Trim a Binary Search Tree] #leetcode
This file contains hidden or 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
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