Skip to content

Commit cb431c5

Browse files
committedJan 11, 2022
feat: add solutions to lc problem: No.0669
No.0669.Trim a Binary Search Tree
1 parent 09bc93a commit cb431c5

File tree

6 files changed

+300
-9
lines changed

6 files changed

+300
-9
lines changed
 

‎solution/0600-0699/0669.Trim a Binary Search Tree/README.md

+104-1
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,118 @@
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```python
73-
73+
# Definition for a binary tree node.
74+
# class TreeNode:
75+
# def __init__(self, val=0, left=None, right=None):
76+
# self.val = val
77+
# self.left = left
78+
# self.right = right
79+
class Solution:
80+
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
81+
def dfs(root):
82+
if root is None:
83+
return root
84+
if root.val > high:
85+
return dfs(root.left)
86+
if root.val < low:
87+
return dfs(root.right)
88+
root.left = dfs(root.left)
89+
root.right = dfs(root.right)
90+
return root
91+
92+
return dfs(root)
7493
```
7594

7695
### **Java**
7796

7897
<!-- 这里可写当前语言的特殊实现逻辑 -->
7998

8099
```java
100+
/**
101+
* Definition for a binary tree node.
102+
* public class TreeNode {
103+
* int val;
104+
* TreeNode left;
105+
* TreeNode right;
106+
* TreeNode() {}
107+
* TreeNode(int val) { this.val = val; }
108+
* TreeNode(int val, TreeNode left, TreeNode right) {
109+
* this.val = val;
110+
* this.left = left;
111+
* this.right = right;
112+
* }
113+
* }
114+
*/
115+
class Solution {
116+
public TreeNode trimBST(TreeNode root, int low, int high) {
117+
if (root == null) {
118+
return root;
119+
}
120+
if (root.val > high) {
121+
return trimBST(root.left, low, high);
122+
}
123+
if (root.val < low) {
124+
return trimBST(root.right, low, high);
125+
}
126+
root.left = trimBST(root.left, low, high);
127+
root.right = trimBST(root.right, low, high);
128+
return root;
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
/**
137+
* Definition for a binary tree node.
138+
* struct TreeNode {
139+
* int val;
140+
* TreeNode *left;
141+
* TreeNode *right;
142+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
143+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
144+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
145+
* };
146+
*/
147+
class Solution {
148+
public:
149+
TreeNode* trimBST(TreeNode* root, int low, int high) {
150+
if (!root) return root;
151+
if (root->val > high) return trimBST(root->left, low, high);
152+
if (root->val < low) return trimBST(root->right, low, high);
153+
root->left = trimBST(root->left, low, high);
154+
root->right = trimBST(root->right, low, high);
155+
return root;
156+
}
157+
};
158+
```
81159
160+
### **Go**
161+
162+
```go
163+
/**
164+
* Definition for a binary tree node.
165+
* type TreeNode struct {
166+
* Val int
167+
* Left *TreeNode
168+
* Right *TreeNode
169+
* }
170+
*/
171+
func trimBST(root *TreeNode, low int, high int) *TreeNode {
172+
if root == nil {
173+
return root
174+
}
175+
if root.Val > high {
176+
return trimBST(root.Left, low, high)
177+
}
178+
if root.Val < low {
179+
return trimBST(root.Right, low, high)
180+
}
181+
root.Left = trimBST(root.Left, low, high)
182+
root.Right = trimBST(root.Right, low, high)
183+
return root
184+
}
82185
```
83186

84187
### **...**

‎solution/0600-0699/0669.Trim a Binary Search Tree/README_EN.md

+104-1
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,116 @@
6262
### **Python3**
6363

6464
```python
65-
65+
# Definition for a binary tree node.
66+
# class TreeNode:
67+
# def __init__(self, val=0, left=None, right=None):
68+
# self.val = val
69+
# self.left = left
70+
# self.right = right
71+
class Solution:
72+
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
73+
def dfs(root):
74+
if root is None:
75+
return root
76+
if root.val > high:
77+
return dfs(root.left)
78+
if root.val < low:
79+
return dfs(root.right)
80+
root.left = dfs(root.left)
81+
root.right = dfs(root.right)
82+
return root
83+
84+
return dfs(root)
6685
```
6786

6887
### **Java**
6988

7089
```java
90+
/**
91+
* Definition for a binary tree node.
92+
* public class TreeNode {
93+
* int val;
94+
* TreeNode left;
95+
* TreeNode right;
96+
* TreeNode() {}
97+
* TreeNode(int val) { this.val = val; }
98+
* TreeNode(int val, TreeNode left, TreeNode right) {
99+
* this.val = val;
100+
* this.left = left;
101+
* this.right = right;
102+
* }
103+
* }
104+
*/
105+
class Solution {
106+
public TreeNode trimBST(TreeNode root, int low, int high) {
107+
if (root == null) {
108+
return root;
109+
}
110+
if (root.val > high) {
111+
return trimBST(root.left, low, high);
112+
}
113+
if (root.val < low) {
114+
return trimBST(root.right, low, high);
115+
}
116+
root.left = trimBST(root.left, low, high);
117+
root.right = trimBST(root.right, low, high);
118+
return root;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
/**
127+
* Definition for a binary tree node.
128+
* struct TreeNode {
129+
* int val;
130+
* TreeNode *left;
131+
* TreeNode *right;
132+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
133+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
134+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
135+
* };
136+
*/
137+
class Solution {
138+
public:
139+
TreeNode* trimBST(TreeNode* root, int low, int high) {
140+
if (!root) return root;
141+
if (root->val > high) return trimBST(root->left, low, high);
142+
if (root->val < low) return trimBST(root->right, low, high);
143+
root->left = trimBST(root->left, low, high);
144+
root->right = trimBST(root->right, low, high);
145+
return root;
146+
}
147+
};
148+
```
71149
150+
### **Go**
151+
152+
```go
153+
/**
154+
* Definition for a binary tree node.
155+
* type TreeNode struct {
156+
* Val int
157+
* Left *TreeNode
158+
* Right *TreeNode
159+
* }
160+
*/
161+
func trimBST(root *TreeNode, low int, high int) *TreeNode {
162+
if root == nil {
163+
return root
164+
}
165+
if root.Val > high {
166+
return trimBST(root.Left, low, high)
167+
}
168+
if root.Val < low {
169+
return trimBST(root.Right, low, high)
170+
}
171+
root.Left = trimBST(root.Left, low, high)
172+
root.Right = trimBST(root.Right, low, high)
173+
return root
174+
}
72175
```
73176

74177
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* trimBST(TreeNode* root, int low, int high) {
15+
if (!root) return root;
16+
if (root->val > high) return trimBST(root->left, low, high);
17+
if (root->val < low) return trimBST(root->right, low, high);
18+
root->left = trimBST(root->left, low, high);
19+
root->right = trimBST(root->right, low, high);
20+
return root;
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func trimBST(root *TreeNode, low int, high int) *TreeNode {
10+
if root == nil {
11+
return root
12+
}
13+
if root.Val > high {
14+
return trimBST(root.Left, low, high)
15+
}
16+
if root.Val < low {
17+
return trimBST(root.Right, low, high)
18+
}
19+
root.Left = trimBST(root.Left, low, high)
20+
root.Right = trimBST(root.Right, low, high)
21+
return root
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
116
class Solution {
2-
public TreeNode trimBST(TreeNode root, int L, int R) {
3-
if (root == null) return null;
4-
if (root.val < L) return trimBST(root.right, L, R);
5-
if (root.val > R) return trimBST(root.left, L, R);
6-
root.left = trimBST(root.left, L, R);
7-
root.right = trimBST(root.right, L, R);
17+
public TreeNode trimBST(TreeNode root, int low, int high) {
18+
if (root == null) {
19+
return root;
20+
}
21+
if (root.val > high) {
22+
return trimBST(root.left, low, high);
23+
}
24+
if (root.val < low) {
25+
return trimBST(root.right, low, high);
26+
}
27+
root.left = trimBST(root.left, low, high);
28+
root.right = trimBST(root.right, low, high);
829
return root;
930
}
10-
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
9+
def dfs(root):
10+
if root is None:
11+
return root
12+
if root.val > high:
13+
return dfs(root.left)
14+
if root.val < low:
15+
return dfs(root.right)
16+
root.left = dfs(root.left)
17+
root.right = dfs(root.right)
18+
return root
19+
20+
return dfs(root)

0 commit comments

Comments
 (0)
Please sign in to comment.