Skip to content

Commit 930dae6

Browse files
committed
feat: add solutions to lc problem: No.0776
No.0776.Split BST
1 parent 9bec4c9 commit 930dae6

File tree

11 files changed

+676
-5
lines changed

11 files changed

+676
-5
lines changed

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

+78
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,84 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode {
350350
}
351351
```
352352

353+
### **JavaScript**
354+
355+
```js
356+
/**
357+
* Definition for a binary tree node.
358+
* function TreeNode(val, left, right) {
359+
* this.val = (val===undefined ? 0 : val)
360+
* this.left = (left===undefined ? null : left)
361+
* this.right = (right===undefined ? null : right)
362+
* }
363+
*/
364+
/**
365+
* @param {TreeNode} root
366+
* @param {number} low
367+
* @param {number} high
368+
* @return {TreeNode}
369+
*/
370+
var trimBST = function (root, low, high) {
371+
function dfs(root) {
372+
if (!root) {
373+
return root;
374+
}
375+
if (root.val < low) {
376+
return dfs(root.right);
377+
}
378+
if (root.val > high) {
379+
return dfs(root.left);
380+
}
381+
root.left = dfs(root.left);
382+
root.right = dfs(root.right);
383+
return root;
384+
}
385+
return dfs(root);
386+
};
387+
```
388+
389+
```js
390+
/**
391+
* Definition for a binary tree node.
392+
* function TreeNode(val, left, right) {
393+
* this.val = (val===undefined ? 0 : val)
394+
* this.left = (left===undefined ? null : left)
395+
* this.right = (right===undefined ? null : right)
396+
* }
397+
*/
398+
/**
399+
* @param {TreeNode} root
400+
* @param {number} low
401+
* @param {number} high
402+
* @return {TreeNode}
403+
*/
404+
var trimBST = function (root, low, high) {
405+
while (root && (root.val < low || root.val > high)) {
406+
root = root.val < low ? root.right : root.left;
407+
}
408+
if (!root) {
409+
return root;
410+
}
411+
let node = root;
412+
while (node.left) {
413+
if (node.left.val < low) {
414+
node.left = node.left.right;
415+
} else {
416+
node = node.left;
417+
}
418+
}
419+
node = root;
420+
while (node.right) {
421+
if (node.right.val > high) {
422+
node.right = node.right.left;
423+
} else {
424+
node = node.right;
425+
}
426+
}
427+
return root;
428+
};
429+
```
430+
353431
### **...**
354432

355433
```

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

+78
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,84 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode {
312312
}
313313
```
314314

315+
### **JavaScript**
316+
317+
```js
318+
/**
319+
* Definition for a binary tree node.
320+
* function TreeNode(val, left, right) {
321+
* this.val = (val===undefined ? 0 : val)
322+
* this.left = (left===undefined ? null : left)
323+
* this.right = (right===undefined ? null : right)
324+
* }
325+
*/
326+
/**
327+
* @param {TreeNode} root
328+
* @param {number} low
329+
* @param {number} high
330+
* @return {TreeNode}
331+
*/
332+
var trimBST = function (root, low, high) {
333+
function dfs(root) {
334+
if (!root) {
335+
return root;
336+
}
337+
if (root.val < low) {
338+
return dfs(root.right);
339+
}
340+
if (root.val > high) {
341+
return dfs(root.left);
342+
}
343+
root.left = dfs(root.left);
344+
root.right = dfs(root.right);
345+
return root;
346+
}
347+
return dfs(root);
348+
};
349+
```
350+
351+
```js
352+
/**
353+
* Definition for a binary tree node.
354+
* function TreeNode(val, left, right) {
355+
* this.val = (val===undefined ? 0 : val)
356+
* this.left = (left===undefined ? null : left)
357+
* this.right = (right===undefined ? null : right)
358+
* }
359+
*/
360+
/**
361+
* @param {TreeNode} root
362+
* @param {number} low
363+
* @param {number} high
364+
* @return {TreeNode}
365+
*/
366+
var trimBST = function (root, low, high) {
367+
while (root && (root.val < low || root.val > high)) {
368+
root = root.val < low ? root.right : root.left;
369+
}
370+
if (!root) {
371+
return root;
372+
}
373+
let node = root;
374+
while (node.left) {
375+
if (node.left.val < low) {
376+
node.left = node.left.right;
377+
} else {
378+
node = node.left;
379+
}
380+
}
381+
node = root;
382+
while (node.right) {
383+
if (node.right.val > high) {
384+
node.right = node.right.left;
385+
} else {
386+
node = node.right;
387+
}
388+
}
389+
return root;
390+
};
391+
```
392+
315393
### **...**
316394

317395
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {number} low
12+
* @param {number} high
13+
* @return {TreeNode}
14+
*/
15+
var trimBST = function (root, low, high) {
16+
function dfs(root) {
17+
if (!root) {
18+
return root;
19+
}
20+
if (root.val < low) {
21+
return dfs(root.right);
22+
}
23+
if (root.val > high) {
24+
return dfs(root.left);
25+
}
26+
root.left = dfs(root.left);
27+
root.right = dfs(root.right);
28+
return root;
29+
}
30+
return dfs(root);
31+
};

solution/0700-0799/0776.Split BST/README.md

+173-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,194 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:递归**
47+
48+
判断 `root` 节点的情况:
49+
50+
-`root` 为空,直接返回 `[null, null]`
51+
-`root.val <= target`,说明 `root` 及其左孩子所有节点的值均小于等于 `target`,那么我们递归 `root.right`,得到 `ans`。然后将 `root.right` 指向 `ans[0]`,最后返回 `[root, ans[1]]`
52+
-`root.val > target`,说明 `root` 及其右孩子所有节点的值均大于 `target`,那么我们递归 `root.left`,得到 `ans`。然后将 `root.left` 指向 `ans[1]`,最后返回 `[ans[0], root]`
53+
54+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉搜索树的节点个数。
55+
4656
<!-- tabs:start -->
4757

4858
### **Python3**
4959

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

5262
```python
53-
63+
# Definition for a binary tree node.
64+
# class TreeNode:
65+
# def __init__(self, val=0, left=None, right=None):
66+
# self.val = val
67+
# self.left = left
68+
# self.right = right
69+
class Solution:
70+
def splitBST(self, root: Optional[TreeNode], target: int) -> List[Optional[TreeNode]]:
71+
def dfs(root):
72+
if root is None:
73+
return [None, None]
74+
if root.val <= target:
75+
l, r = dfs(root.right)
76+
root.right = l
77+
return [root, r]
78+
else:
79+
l, r = dfs(root.left)
80+
root.left = r
81+
return [l, root]
82+
83+
return dfs(root)
5484
```
5585

5686
### **Java**
5787

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

6090
```java
91+
/**
92+
* Definition for a binary tree node.
93+
* public class TreeNode {
94+
* int val;
95+
* TreeNode left;
96+
* TreeNode right;
97+
* TreeNode() {}
98+
* TreeNode(int val) { this.val = val; }
99+
* TreeNode(int val, TreeNode left, TreeNode right) {
100+
* this.val = val;
101+
* this.left = left;
102+
* this.right = right;
103+
* }
104+
* }
105+
*/
106+
class Solution {
107+
private int t;
108+
109+
public TreeNode[] splitBST(TreeNode root, int target) {
110+
t = target;
111+
return dfs(root);
112+
}
113+
114+
private TreeNode[] dfs(TreeNode root) {
115+
if (root == null) {
116+
return new TreeNode[]{null, null};
117+
}
118+
if (root.val <= t) {
119+
TreeNode[] ans = dfs(root.right);
120+
root.right = ans[0];
121+
ans[0] = root;
122+
return ans;
123+
} else {
124+
TreeNode[] ans = dfs(root.left);
125+
root.left = ans[1];
126+
ans[1] = root;
127+
return ans;
128+
}
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+
int t;
150+
151+
vector<TreeNode*> splitBST(TreeNode* root, int target) {
152+
t = target;
153+
return dfs(root);
154+
}
155+
156+
vector<TreeNode*> dfs(TreeNode* root) {
157+
if (!root) return {nullptr, nullptr};
158+
if (root->val <= t) {
159+
auto ans = dfs(root->right);
160+
root->right = ans[0];
161+
ans[0] = root;
162+
return ans;
163+
} else {
164+
auto ans = dfs(root->left);
165+
root->left = ans[1];
166+
ans[1] = root;
167+
return ans;
168+
}
169+
}
170+
};
171+
```
172+
173+
### **Go**
174+
175+
```go
176+
/**
177+
* Definition for a binary tree node.
178+
* type TreeNode struct {
179+
* Val int
180+
* Left *TreeNode
181+
* Right *TreeNode
182+
* }
183+
*/
184+
func splitBST(root *TreeNode, target int) []*TreeNode {
185+
if root == nil {
186+
return []*TreeNode{nil, nil}
187+
}
188+
if root.Val <= target {
189+
ans := splitBST(root.Right, target)
190+
root.Right = ans[0]
191+
ans[0] = root
192+
return ans
193+
} else {
194+
ans := splitBST(root.Left, target)
195+
root.Left = ans[1]
196+
ans[1] = root
197+
return ans
198+
}
199+
}
200+
```
61201

202+
### **JavaScript**
203+
204+
```js
205+
/**
206+
* Definition for a binary tree node.
207+
* function TreeNode(val, left, right) {
208+
* this.val = (val===undefined ? 0 : val)
209+
* this.left = (left===undefined ? null : left)
210+
* this.right = (right===undefined ? null : right)
211+
* }
212+
*/
213+
/**
214+
* @param {TreeNode} root
215+
* @param {number} target
216+
* @return {TreeNode[]}
217+
*/
218+
var splitBST = function (root, target) {
219+
let ans = [null, null];
220+
if (!root) {
221+
return ans;
222+
}
223+
if (root.val <= target) {
224+
ans = splitBST(root.right, target);
225+
root.right = ans[0];
226+
ans[0] = root;
227+
} else {
228+
ans = splitBST(root.left, target);
229+
root.left = ans[1];
230+
ans[1] = root;
231+
}
232+
return ans;
233+
};
62234
```
63235

64236
### **...**

0 commit comments

Comments
 (0)