Skip to content

Commit 8ab64b0

Browse files
committed
solve problem Trim A Binary Search Tree
1 parent f7cf16c commit 8ab64b0

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ All solutions will be accepted!
6464
|257|[Binary Tree Paths](https://leetcode-cn.com/problems/binary-tree-paths/description/)|[java/py/js](./algorithms/BinaryTreePaths)|Easy|
6565
|496|[Next Greater Element I](https://leetcode-cn.com/problems/next-greater-element-i/description/)|[java/py/js](./algorithms/NextGeraterElementI)|Easy|
6666
|766|[Toeplitz Matrix](https://leetcode-cn.com/problems/toeplitz-matrix/description/)|[java/py/js](./algorithms/ToeplitzMatrix)|Easy|
67+
|669|[Trim A Binary Search Tree](https://leetcode-cn.com/problems/trim-a-binary-search-tree/description/)|[java/py/js](./algorithms/TrimABinarySearchTree)|Easy|
6768

6869
# Database
6970
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Trim A Binary Search Tree
2+
This problem is easy to solve by recursion
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode trimBST(TreeNode root, int L, int R) {
12+
if (root == null) {
13+
return null;
14+
} else if (root.val > R) {
15+
return trimBST(root.left, L, R);
16+
} else if (root.val < L) {
17+
return trimBST(root.right, L, R);
18+
} else {
19+
root.left = trimBST(root.left, L, R);
20+
root.right = trimBST(root.right, L, R);
21+
return root;
22+
}
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {number} L
11+
* @param {number} R
12+
* @return {TreeNode}
13+
*/
14+
var trimBST = function(root, L, R) {
15+
if (root === null) {
16+
return null
17+
} else if (root.val > R) {
18+
return trimBST(root.left, L, R)
19+
} else if (root.val < L) {
20+
return trimBST(root.right, L, R)
21+
} else {
22+
root.left = trimBST(root.left, L, R)
23+
root.right = trimBST(root.right, L, R)
24+
return root
25+
}
26+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def trimBST(self, root, L, R):
10+
"""
11+
:type root: TreeNode
12+
:type L: int
13+
:type R: int
14+
:rtype: TreeNode
15+
"""
16+
if not root:
17+
return None
18+
elif root.val < L:
19+
return self.trimBST(root.right, L, R)
20+
elif root.val > R:
21+
return self.trimBST(root.left, L, R)
22+
else:
23+
root.left = self.trimBST(root.left, L, R)
24+
root.right = self.trimBST(root.right, L, R)
25+
return root

0 commit comments

Comments
 (0)